Path: blob/master/arch/powerpc/platforms/pasemi/dma_lib.c
26489 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2006-2007 PA Semi, Inc3*4* Common functions for DMA access on PA Semi PWRficient5*/67#include <linux/kernel.h>8#include <linux/export.h>9#include <linux/pci.h>10#include <linux/slab.h>11#include <linux/of.h>12#include <linux/of_address.h>13#include <linux/of_irq.h>14#include <linux/sched.h>1516#include <asm/pasemi_dma.h>1718#define MAX_TXCH 6419#define MAX_RXCH 6420#define MAX_FLAGS 6421#define MAX_FUN 82223static struct pasdma_status *dma_status;2425static void __iomem *iob_regs;26static void __iomem *mac_regs[6];27static void __iomem *dma_regs;2829static int base_hw_irq;3031static int num_txch, num_rxch;3233static struct pci_dev *dma_pdev;3435/* Bitmaps to handle allocation of channels */3637static DECLARE_BITMAP(txch_free, MAX_TXCH);38static DECLARE_BITMAP(rxch_free, MAX_RXCH);39static DECLARE_BITMAP(flags_free, MAX_FLAGS);40static DECLARE_BITMAP(fun_free, MAX_FUN);4142/* pasemi_read_iob_reg - read IOB register43* @reg: Register to read (offset into PCI CFG space)44*/45unsigned int pasemi_read_iob_reg(unsigned int reg)46{47return in_le32(iob_regs+reg);48}49EXPORT_SYMBOL(pasemi_read_iob_reg);5051/* pasemi_write_iob_reg - write IOB register52* @reg: Register to write to (offset into PCI CFG space)53* @val: Value to write54*/55void pasemi_write_iob_reg(unsigned int reg, unsigned int val)56{57out_le32(iob_regs+reg, val);58}59EXPORT_SYMBOL(pasemi_write_iob_reg);6061/* pasemi_read_mac_reg - read MAC register62* @intf: MAC interface63* @reg: Register to read (offset into PCI CFG space)64*/65unsigned int pasemi_read_mac_reg(int intf, unsigned int reg)66{67return in_le32(mac_regs[intf]+reg);68}69EXPORT_SYMBOL(pasemi_read_mac_reg);7071/* pasemi_write_mac_reg - write MAC register72* @intf: MAC interface73* @reg: Register to write to (offset into PCI CFG space)74* @val: Value to write75*/76void pasemi_write_mac_reg(int intf, unsigned int reg, unsigned int val)77{78out_le32(mac_regs[intf]+reg, val);79}80EXPORT_SYMBOL(pasemi_write_mac_reg);8182/* pasemi_read_dma_reg - read DMA register83* @reg: Register to read (offset into PCI CFG space)84*/85unsigned int pasemi_read_dma_reg(unsigned int reg)86{87return in_le32(dma_regs+reg);88}89EXPORT_SYMBOL(pasemi_read_dma_reg);9091/* pasemi_write_dma_reg - write DMA register92* @reg: Register to write to (offset into PCI CFG space)93* @val: Value to write94*/95void pasemi_write_dma_reg(unsigned int reg, unsigned int val)96{97out_le32(dma_regs+reg, val);98}99EXPORT_SYMBOL(pasemi_write_dma_reg);100101static int pasemi_alloc_tx_chan(enum pasemi_dmachan_type type)102{103int bit;104int start, limit;105106switch (type & (TXCHAN_EVT0|TXCHAN_EVT1)) {107case TXCHAN_EVT0:108start = 0;109limit = 10;110break;111case TXCHAN_EVT1:112start = 10;113limit = MAX_TXCH;114break;115default:116start = 0;117limit = MAX_TXCH;118break;119}120retry:121bit = find_next_bit(txch_free, MAX_TXCH, start);122if (bit >= limit)123return -ENOSPC;124if (!test_and_clear_bit(bit, txch_free))125goto retry;126127return bit;128}129130static void pasemi_free_tx_chan(int chan)131{132BUG_ON(test_bit(chan, txch_free));133set_bit(chan, txch_free);134}135136static int pasemi_alloc_rx_chan(void)137{138int bit;139retry:140bit = find_first_bit(rxch_free, MAX_RXCH);141if (bit >= MAX_TXCH)142return -ENOSPC;143if (!test_and_clear_bit(bit, rxch_free))144goto retry;145146return bit;147}148149static void pasemi_free_rx_chan(int chan)150{151BUG_ON(test_bit(chan, rxch_free));152set_bit(chan, rxch_free);153}154155/* pasemi_dma_alloc_chan - Allocate a DMA channel156* @type: Type of channel to allocate157* @total_size: Total size of structure to allocate (to allow for more158* room behind the structure to be used by the client)159* @offset: Offset in bytes from start of the total structure to the beginning160* of struct pasemi_dmachan. Needed when struct pasemi_dmachan is161* not the first member of the client structure.162*163* pasemi_dma_alloc_chan allocates a DMA channel for use by a client. The164* type argument specifies whether it's a RX or TX channel, and in the case165* of TX channels which group it needs to belong to (if any).166*167* Returns a pointer to the total structure allocated on success, NULL168* on failure.169*/170void *pasemi_dma_alloc_chan(enum pasemi_dmachan_type type,171int total_size, int offset)172{173void *buf;174struct pasemi_dmachan *chan;175int chno;176177BUG_ON(total_size < sizeof(struct pasemi_dmachan));178179buf = kzalloc(total_size, GFP_KERNEL);180181if (!buf)182return NULL;183chan = buf + offset;184185chan->priv = buf;186187switch (type & (TXCHAN|RXCHAN)) {188case RXCHAN:189chno = pasemi_alloc_rx_chan();190chan->chno = chno;191chan->irq = irq_create_mapping(NULL,192base_hw_irq + num_txch + chno);193chan->status = &dma_status->rx_sta[chno];194break;195case TXCHAN:196chno = pasemi_alloc_tx_chan(type);197chan->chno = chno;198chan->irq = irq_create_mapping(NULL, base_hw_irq + chno);199chan->status = &dma_status->tx_sta[chno];200break;201}202203chan->chan_type = type;204205return chan;206}207EXPORT_SYMBOL(pasemi_dma_alloc_chan);208209/* pasemi_dma_free_chan - Free a previously allocated channel210* @chan: Channel to free211*212* Frees a previously allocated channel. It will also deallocate any213* descriptor ring associated with the channel, if allocated.214*/215void pasemi_dma_free_chan(struct pasemi_dmachan *chan)216{217if (chan->ring_virt)218pasemi_dma_free_ring(chan);219220switch (chan->chan_type & (RXCHAN|TXCHAN)) {221case RXCHAN:222pasemi_free_rx_chan(chan->chno);223break;224case TXCHAN:225pasemi_free_tx_chan(chan->chno);226break;227}228229kfree(chan->priv);230}231EXPORT_SYMBOL(pasemi_dma_free_chan);232233/* pasemi_dma_alloc_ring - Allocate descriptor ring for a channel234* @chan: Channel for which to allocate235* @ring_size: Ring size in 64-bit (8-byte) words236*237* Allocate a descriptor ring for a channel. Returns 0 on success, errno238* on failure. The passed in struct pasemi_dmachan is updated with the239* virtual and DMA addresses of the ring.240*/241int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)242{243BUG_ON(chan->ring_virt);244245chan->ring_size = ring_size;246247chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,248ring_size * sizeof(u64),249&chan->ring_dma, GFP_KERNEL);250251if (!chan->ring_virt)252return -ENOMEM;253254return 0;255}256EXPORT_SYMBOL(pasemi_dma_alloc_ring);257258/* pasemi_dma_free_ring - Free an allocated descriptor ring for a channel259* @chan: Channel for which to free the descriptor ring260*261* Frees a previously allocated descriptor ring for a channel.262*/263void pasemi_dma_free_ring(struct pasemi_dmachan *chan)264{265BUG_ON(!chan->ring_virt);266267dma_free_coherent(&dma_pdev->dev, chan->ring_size * sizeof(u64),268chan->ring_virt, chan->ring_dma);269chan->ring_virt = NULL;270chan->ring_size = 0;271chan->ring_dma = 0;272}273EXPORT_SYMBOL(pasemi_dma_free_ring);274275/* pasemi_dma_start_chan - Start a DMA channel276* @chan: Channel to start277* @cmdsta: Additional CCMDSTA/TCMDSTA bits to write278*279* Enables (starts) a DMA channel with optional additional arguments.280*/281void pasemi_dma_start_chan(const struct pasemi_dmachan *chan, const u32 cmdsta)282{283if (chan->chan_type == RXCHAN)284pasemi_write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno),285cmdsta | PAS_DMA_RXCHAN_CCMDSTA_EN);286else287pasemi_write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno),288cmdsta | PAS_DMA_TXCHAN_TCMDSTA_EN);289}290EXPORT_SYMBOL(pasemi_dma_start_chan);291292/* pasemi_dma_stop_chan - Stop a DMA channel293* @chan: Channel to stop294*295* Stops (disables) a DMA channel. This is done by setting the ST bit in the296* CMDSTA register and waiting on the ACT (active) bit to clear, then297* finally disabling the whole channel.298*299* This function will only try for a short while for the channel to stop, if300* it doesn't it will return failure.301*302* Returns 1 on success, 0 on failure.303*/304#define MAX_RETRIES 5000305int pasemi_dma_stop_chan(const struct pasemi_dmachan *chan)306{307int reg, retries;308u32 sta;309310if (chan->chan_type == RXCHAN) {311reg = PAS_DMA_RXCHAN_CCMDSTA(chan->chno);312pasemi_write_dma_reg(reg, PAS_DMA_RXCHAN_CCMDSTA_ST);313for (retries = 0; retries < MAX_RETRIES; retries++) {314sta = pasemi_read_dma_reg(reg);315if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)) {316pasemi_write_dma_reg(reg, 0);317return 1;318}319cond_resched();320}321} else {322reg = PAS_DMA_TXCHAN_TCMDSTA(chan->chno);323pasemi_write_dma_reg(reg, PAS_DMA_TXCHAN_TCMDSTA_ST);324for (retries = 0; retries < MAX_RETRIES; retries++) {325sta = pasemi_read_dma_reg(reg);326if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)) {327pasemi_write_dma_reg(reg, 0);328return 1;329}330cond_resched();331}332}333334return 0;335}336EXPORT_SYMBOL(pasemi_dma_stop_chan);337338/* pasemi_dma_alloc_buf - Allocate a buffer to use for DMA339* @chan: Channel to allocate for340* @size: Size of buffer in bytes341* @handle: DMA handle342*343* Allocate a buffer to be used by the DMA engine for read/write,344* similar to dma_alloc_coherent().345*346* Returns the virtual address of the buffer, or NULL in case of failure.347*/348void *pasemi_dma_alloc_buf(struct pasemi_dmachan *chan, int size,349dma_addr_t *handle)350{351return dma_alloc_coherent(&dma_pdev->dev, size, handle, GFP_KERNEL);352}353EXPORT_SYMBOL(pasemi_dma_alloc_buf);354355/* pasemi_dma_free_buf - Free a buffer used for DMA356* @chan: Channel the buffer was allocated for357* @size: Size of buffer in bytes358* @handle: DMA handle359*360* Frees a previously allocated buffer.361*/362void pasemi_dma_free_buf(struct pasemi_dmachan *chan, int size,363dma_addr_t *handle)364{365dma_free_coherent(&dma_pdev->dev, size, handle, GFP_KERNEL);366}367EXPORT_SYMBOL(pasemi_dma_free_buf);368369/* pasemi_dma_alloc_flag - Allocate a flag (event) for channel synchronization370*371* Allocates a flag for use with channel synchronization (event descriptors).372* Returns allocated flag (0-63), < 0 on error.373*/374int pasemi_dma_alloc_flag(void)375{376int bit;377378retry:379bit = find_first_bit(flags_free, MAX_FLAGS);380if (bit >= MAX_FLAGS)381return -ENOSPC;382if (!test_and_clear_bit(bit, flags_free))383goto retry;384385return bit;386}387EXPORT_SYMBOL(pasemi_dma_alloc_flag);388389390/* pasemi_dma_free_flag - Deallocates a flag (event)391* @flag: Flag number to deallocate392*393* Frees up a flag so it can be reused for other purposes.394*/395void pasemi_dma_free_flag(int flag)396{397BUG_ON(test_bit(flag, flags_free));398BUG_ON(flag >= MAX_FLAGS);399set_bit(flag, flags_free);400}401EXPORT_SYMBOL(pasemi_dma_free_flag);402403404/* pasemi_dma_set_flag - Sets a flag (event) to 1405* @flag: Flag number to set active406*407* Sets the flag provided to 1.408*/409void pasemi_dma_set_flag(int flag)410{411BUG_ON(flag >= MAX_FLAGS);412if (flag < 32)413pasemi_write_dma_reg(PAS_DMA_TXF_SFLG0, 1 << flag);414else415pasemi_write_dma_reg(PAS_DMA_TXF_SFLG1, 1 << flag);416}417EXPORT_SYMBOL(pasemi_dma_set_flag);418419/* pasemi_dma_clear_flag - Sets a flag (event) to 0420* @flag: Flag number to set inactive421*422* Sets the flag provided to 0.423*/424void pasemi_dma_clear_flag(int flag)425{426BUG_ON(flag >= MAX_FLAGS);427if (flag < 32)428pasemi_write_dma_reg(PAS_DMA_TXF_CFLG0, 1 << flag);429else430pasemi_write_dma_reg(PAS_DMA_TXF_CFLG1, 1 << flag);431}432EXPORT_SYMBOL(pasemi_dma_clear_flag);433434/* pasemi_dma_alloc_fun - Allocate a function engine435*436* Allocates a function engine to use for crypto/checksum offload437* Returns allocated engine (0-8), < 0 on error.438*/439int pasemi_dma_alloc_fun(void)440{441int bit;442443retry:444bit = find_first_bit(fun_free, MAX_FLAGS);445if (bit >= MAX_FLAGS)446return -ENOSPC;447if (!test_and_clear_bit(bit, fun_free))448goto retry;449450return bit;451}452EXPORT_SYMBOL(pasemi_dma_alloc_fun);453454455/* pasemi_dma_free_fun - Deallocates a function engine456* @flag: Engine number to deallocate457*458* Frees up a function engine so it can be used for other purposes.459*/460void pasemi_dma_free_fun(int fun)461{462BUG_ON(test_bit(fun, fun_free));463BUG_ON(fun >= MAX_FLAGS);464set_bit(fun, fun_free);465}466EXPORT_SYMBOL(pasemi_dma_free_fun);467468469static void *map_onedev(struct pci_dev *p, int index)470{471struct device_node *dn;472void __iomem *ret;473474dn = pci_device_to_OF_node(p);475if (!dn)476goto fallback;477478ret = of_iomap(dn, index);479if (!ret)480goto fallback;481482return ret;483fallback:484/* This is hardcoded and ugly, but we have some firmware versions485* that don't provide the register space in the device tree. Luckily486* they are at well-known locations so we can just do the math here.487*/488return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);489}490491/* pasemi_dma_init - Initialize the PA Semi DMA library492*493* This function initializes the DMA library. It must be called before494* any other function in the library.495*496* Returns 0 on success, errno on failure.497*/498int pasemi_dma_init(void)499{500static DEFINE_SPINLOCK(init_lock);501struct pci_dev *iob_pdev;502struct pci_dev *pdev;503struct resource res;504struct device_node *dn;505int i, intf, err = 0;506unsigned long timeout;507u32 tmp;508509if (!machine_is(pasemi))510return -ENODEV;511512spin_lock(&init_lock);513514/* Make sure we haven't already initialized */515if (dma_pdev)516goto out;517518iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);519if (!iob_pdev) {520BUG();521pr_warn("Can't find I/O Bridge\n");522err = -ENODEV;523goto out;524}525iob_regs = map_onedev(iob_pdev, 0);526527dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);528if (!dma_pdev) {529BUG();530pr_warn("Can't find DMA controller\n");531err = -ENODEV;532goto out;533}534dma_regs = map_onedev(dma_pdev, 0);535base_hw_irq = virq_to_hw(dma_pdev->irq);536537pci_read_config_dword(dma_pdev, PAS_DMA_CAP_TXCH, &tmp);538num_txch = (tmp & PAS_DMA_CAP_TXCH_TCHN_M) >> PAS_DMA_CAP_TXCH_TCHN_S;539540pci_read_config_dword(dma_pdev, PAS_DMA_CAP_RXCH, &tmp);541num_rxch = (tmp & PAS_DMA_CAP_RXCH_RCHN_M) >> PAS_DMA_CAP_RXCH_RCHN_S;542543intf = 0;544for (pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa006, NULL);545pdev;546pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa006, pdev))547mac_regs[intf++] = map_onedev(pdev, 0);548549pci_dev_put(pdev);550551for (pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa005, NULL);552pdev;553pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa005, pdev))554mac_regs[intf++] = map_onedev(pdev, 0);555556pci_dev_put(pdev);557558dn = pci_device_to_OF_node(iob_pdev);559if (dn)560err = of_address_to_resource(dn, 1, &res);561if (!dn || err) {562/* Fallback for old firmware */563res.start = 0xfd800000;564res.end = res.start + 0x1000;565}566dma_status = ioremap_cache(res.start, resource_size(&res));567pci_dev_put(iob_pdev);568569for (i = 0; i < MAX_TXCH; i++)570__set_bit(i, txch_free);571572for (i = 0; i < MAX_RXCH; i++)573__set_bit(i, rxch_free);574575timeout = jiffies + HZ;576pasemi_write_dma_reg(PAS_DMA_COM_RXCMD, 0);577while (pasemi_read_dma_reg(PAS_DMA_COM_RXSTA) & 1) {578if (time_after(jiffies, timeout)) {579pr_warn("Warning: Could not disable RX section\n");580break;581}582}583584timeout = jiffies + HZ;585pasemi_write_dma_reg(PAS_DMA_COM_TXCMD, 0);586while (pasemi_read_dma_reg(PAS_DMA_COM_TXSTA) & 1) {587if (time_after(jiffies, timeout)) {588pr_warn("Warning: Could not disable TX section\n");589break;590}591}592593/* setup resource allocations for the different DMA sections */594tmp = pasemi_read_dma_reg(PAS_DMA_COM_CFG);595pasemi_write_dma_reg(PAS_DMA_COM_CFG, tmp | 0x18000000);596597/* enable tx section */598pasemi_write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);599600/* enable rx section */601pasemi_write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);602603for (i = 0; i < MAX_FLAGS; i++)604__set_bit(i, flags_free);605606for (i = 0; i < MAX_FUN; i++)607__set_bit(i, fun_free);608609/* clear all status flags */610pasemi_write_dma_reg(PAS_DMA_TXF_CFLG0, 0xffffffff);611pasemi_write_dma_reg(PAS_DMA_TXF_CFLG1, 0xffffffff);612613pr_info("PA Semi PWRficient DMA library initialized "614"(%d tx, %d rx channels)\n", num_txch, num_rxch);615616out:617spin_unlock(&init_lock);618return err;619}620EXPORT_SYMBOL(pasemi_dma_init);621622623