rte_mb_a_pci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * arch/v850/kernel/mb_a_pci.c -- PCI support for Midas lab RTE-MOTHER-A board
  3. *
  4. * Copyright (C) 2001,02,03,05 NEC Electronics Corporation
  5. * Copyright (C) 2001,02,03,05 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #include <linux/config.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/pci.h>
  20. #include <asm/machdep.h>
  21. /* __nomods_init is like __devinit, but is a no-op when modules are enabled.
  22. This is used by some routines that can be called either during boot
  23. or by a module. */
  24. #ifdef CONFIG_MODULES
  25. #define __nomods_init /*nothing*/
  26. #else
  27. #define __nomods_init __devinit
  28. #endif
  29. /* PCI devices on the Mother-A board can only do DMA to/from the MB SRAM
  30. (the RTE-V850E/MA1-CB cpu board doesn't support PCI access to
  31. CPU-board memory), and since linux DMA buffers are allocated in
  32. normal kernel memory, we basically have to copy DMA blocks around
  33. (this is like a `bounce buffer'). When a DMA block is `mapped', we
  34. allocate an identically sized block in MB SRAM, and if we're doing
  35. output to the device, copy the CPU-memory block to the MB-SRAM block.
  36. When an active block is `unmapped', we will copy the block back to
  37. CPU memory if necessary, and then deallocate the MB SRAM block.
  38. Ack. */
  39. /* Where the motherboard SRAM is in the PCI-bus address space (the
  40. first 512K of it is also mapped at PCI address 0). */
  41. #define PCI_MB_SRAM_ADDR 0x800000
  42. /* Convert CPU-view MB SRAM address to/from PCI-view addresses of the
  43. same memory. */
  44. #define MB_SRAM_TO_PCI(mb_sram_addr) \
  45. ((dma_addr_t)mb_sram_addr - MB_A_SRAM_ADDR + PCI_MB_SRAM_ADDR)
  46. #define PCI_TO_MB_SRAM(pci_addr) \
  47. (void *)(pci_addr - PCI_MB_SRAM_ADDR + MB_A_SRAM_ADDR)
  48. static void pcibios_assign_resources (void);
  49. struct mb_pci_dev_irq {
  50. unsigned dev; /* PCI device number */
  51. unsigned irq_base; /* First IRQ */
  52. unsigned query_pin; /* True if we should read the device's
  53. Interrupt Pin info, and allocate
  54. interrupt IRQ_BASE + PIN. */
  55. };
  56. /* PCI interrupts are mapped statically to GBUS interrupts. */
  57. static struct mb_pci_dev_irq mb_pci_dev_irqs[] = {
  58. /* Motherboard SB82558 ethernet controller */
  59. { 10, IRQ_MB_A_LAN, 0 },
  60. /* PCI slot 1 */
  61. { 8, IRQ_MB_A_PCI1(0), 1 },
  62. /* PCI slot 2 */
  63. { 9, IRQ_MB_A_PCI2(0), 1 }
  64. };
  65. #define NUM_MB_PCI_DEV_IRQS \
  66. (sizeof mb_pci_dev_irqs / sizeof mb_pci_dev_irqs[0])
  67. /* PCI configuration primitives. */
  68. #define CONFIG_DMCFGA(bus, devfn, offs) \
  69. (0x80000000 \
  70. | ((offs) & ~0x3) \
  71. | ((devfn) << 8) \
  72. | ((bus)->number << 16))
  73. static int
  74. mb_pci_read (struct pci_bus *bus, unsigned devfn, int offs, int size, u32 *rval)
  75. {
  76. u32 addr;
  77. int flags;
  78. local_irq_save (flags);
  79. MB_A_PCI_PCICR = 0x7;
  80. MB_A_PCI_DMCFGA = CONFIG_DMCFGA (bus, devfn, offs);
  81. addr = MB_A_PCI_IO_ADDR + (offs & 0x3);
  82. switch (size) {
  83. case 1: *rval = *(volatile u8 *)addr; break;
  84. case 2: *rval = *(volatile u16 *)addr; break;
  85. case 4: *rval = *(volatile u32 *)addr; break;
  86. }
  87. if (MB_A_PCI_PCISR & 0x2000) {
  88. MB_A_PCI_PCISR = 0x2000;
  89. *rval = ~0;
  90. }
  91. MB_A_PCI_DMCFGA = 0;
  92. local_irq_restore (flags);
  93. return PCIBIOS_SUCCESSFUL;
  94. }
  95. static int
  96. mb_pci_write (struct pci_bus *bus, unsigned devfn, int offs, int size, u32 val)
  97. {
  98. u32 addr;
  99. int flags;
  100. local_irq_save (flags);
  101. MB_A_PCI_PCICR = 0x7;
  102. MB_A_PCI_DMCFGA = CONFIG_DMCFGA (bus, devfn, offs);
  103. addr = MB_A_PCI_IO_ADDR + (offs & 0x3);
  104. switch (size) {
  105. case 1: *(volatile u8 *)addr = val; break;
  106. case 2: *(volatile u16 *)addr = val; break;
  107. case 4: *(volatile u32 *)addr = val; break;
  108. }
  109. if (MB_A_PCI_PCISR & 0x2000)
  110. MB_A_PCI_PCISR = 0x2000;
  111. MB_A_PCI_DMCFGA = 0;
  112. local_irq_restore (flags);
  113. return PCIBIOS_SUCCESSFUL;
  114. }
  115. static struct pci_ops mb_pci_config_ops = {
  116. .read = mb_pci_read,
  117. .write = mb_pci_write,
  118. };
  119. /* PCI Initialization. */
  120. static struct pci_bus *mb_pci_bus = 0;
  121. /* Do initial PCI setup. */
  122. static int __devinit pcibios_init (void)
  123. {
  124. u32 id = MB_A_PCI_PCIHIDR;
  125. u16 vendor = id & 0xFFFF;
  126. u16 device = (id >> 16) & 0xFFFF;
  127. if (vendor == PCI_VENDOR_ID_PLX && device == PCI_DEVICE_ID_PLX_9080) {
  128. printk (KERN_INFO
  129. "PCI: PLX Technology PCI9080 HOST/PCI bridge\n");
  130. MB_A_PCI_PCICR = 0x147;
  131. MB_A_PCI_PCIBAR0 = 0x007FFF00;
  132. MB_A_PCI_PCIBAR1 = 0x0000FF00;
  133. MB_A_PCI_PCIBAR2 = 0x00800000;
  134. MB_A_PCI_PCILTR = 0x20;
  135. MB_A_PCI_PCIPBAM |= 0x3;
  136. MB_A_PCI_PCISR = ~0; /* Clear errors. */
  137. /* Reprogram the motherboard's IO/config address space,
  138. as we don't support the GCS7 address space that the
  139. default uses. */
  140. /* Significant address bits used for decoding PCI GCS5 space
  141. accessess. */
  142. MB_A_PCI_DMRR = ~(MB_A_PCI_MEM_SIZE - 1);
  143. /* I don't understand this, but the SolutionGear example code
  144. uses such an offset, and it doesn't work without it. XXX */
  145. #if GCS5_SIZE == 0x00800000
  146. #define GCS5_CFG_OFFS 0x00800000
  147. #else
  148. #define GCS5_CFG_OFFS 0
  149. #endif
  150. /* Address bit values for matching. Note that we have to give
  151. the address from the motherboard's point of view, which is
  152. different than the CPU's. */
  153. /* PCI memory space. */
  154. MB_A_PCI_DMLBAM = GCS5_CFG_OFFS + 0x0;
  155. /* PCI I/O space. */
  156. MB_A_PCI_DMLBAI =
  157. GCS5_CFG_OFFS + (MB_A_PCI_IO_ADDR - GCS5_ADDR);
  158. mb_pci_bus = pci_scan_bus (0, &mb_pci_config_ops, 0);
  159. pcibios_assign_resources ();
  160. } else
  161. printk (KERN_ERR "PCI: HOST/PCI bridge not found\n");
  162. return 0;
  163. }
  164. subsys_initcall (pcibios_init);
  165. char __devinit *pcibios_setup (char *option)
  166. {
  167. /* Don't handle any options. */
  168. return option;
  169. }
  170. int __nomods_init pcibios_enable_device (struct pci_dev *dev, int mask)
  171. {
  172. u16 cmd, old_cmd;
  173. int idx;
  174. struct resource *r;
  175. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  176. old_cmd = cmd;
  177. for (idx = 0; idx < 6; idx++) {
  178. r = &dev->resource[idx];
  179. if (!r->start && r->end) {
  180. printk(KERN_ERR "PCI: Device %s not available because "
  181. "of resource collisions\n", pci_name(dev));
  182. return -EINVAL;
  183. }
  184. if (r->flags & IORESOURCE_IO)
  185. cmd |= PCI_COMMAND_IO;
  186. if (r->flags & IORESOURCE_MEM)
  187. cmd |= PCI_COMMAND_MEMORY;
  188. }
  189. if (cmd != old_cmd) {
  190. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  191. pci_name(dev), old_cmd, cmd);
  192. pci_write_config_word(dev, PCI_COMMAND, cmd);
  193. }
  194. return 0;
  195. }
  196. /* Resource allocation. */
  197. static void __devinit pcibios_assign_resources (void)
  198. {
  199. struct pci_dev *dev = NULL;
  200. struct resource *r;
  201. for_each_pci_dev(dev) {
  202. unsigned di_num;
  203. unsigned class = dev->class >> 8;
  204. if (class && class != PCI_CLASS_BRIDGE_HOST) {
  205. unsigned r_num;
  206. for(r_num = 0; r_num < 6; r_num++) {
  207. r = &dev->resource[r_num];
  208. if (!r->start && r->end)
  209. pci_assign_resource (dev, r_num);
  210. }
  211. }
  212. /* Assign interrupts. */
  213. for (di_num = 0; di_num < NUM_MB_PCI_DEV_IRQS; di_num++) {
  214. struct mb_pci_dev_irq *di = &mb_pci_dev_irqs[di_num];
  215. if (di->dev == PCI_SLOT (dev->devfn)) {
  216. unsigned irq = di->irq_base;
  217. if (di->query_pin) {
  218. /* Find out which interrupt pin
  219. this device uses (each PCI
  220. slot has 4). */
  221. u8 irq_pin;
  222. pci_read_config_byte (dev,
  223. PCI_INTERRUPT_PIN,
  224. &irq_pin);
  225. if (irq_pin == 0)
  226. /* Doesn't use interrupts. */
  227. continue;
  228. else
  229. irq += irq_pin - 1;
  230. }
  231. pcibios_update_irq (dev, irq);
  232. }
  233. }
  234. }
  235. }
  236. void __devinit pcibios_update_irq (struct pci_dev *dev, int irq)
  237. {
  238. dev->irq = irq;
  239. pci_write_config_byte (dev, PCI_INTERRUPT_LINE, irq);
  240. }
  241. void __devinit
  242. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  243. struct resource *res)
  244. {
  245. unsigned long offset = 0;
  246. if (res->flags & IORESOURCE_IO) {
  247. offset = MB_A_PCI_IO_ADDR;
  248. } else if (res->flags & IORESOURCE_MEM) {
  249. offset = MB_A_PCI_MEM_ADDR;
  250. }
  251. region->start = res->start - offset;
  252. region->end = res->end - offset;
  253. }
  254. /* Stubs for things we don't use. */
  255. /* Called after each bus is probed, but before its children are examined. */
  256. void pcibios_fixup_bus(struct pci_bus *b)
  257. {
  258. }
  259. void
  260. pcibios_align_resource (void *data, struct resource *res,
  261. unsigned long size, unsigned long align)
  262. {
  263. }
  264. void pcibios_set_master (struct pci_dev *dev)
  265. {
  266. }
  267. /* Mother-A SRAM memory allocation. This is a simple first-fit allocator. */
  268. /* A memory free-list node. */
  269. struct mb_sram_free_area {
  270. void *mem;
  271. unsigned long size;
  272. struct mb_sram_free_area *next;
  273. };
  274. /* The tail of the free-list, which starts out containing all the SRAM. */
  275. static struct mb_sram_free_area mb_sram_free_tail = {
  276. (void *)MB_A_SRAM_ADDR, MB_A_SRAM_SIZE, 0
  277. };
  278. /* The free-list. */
  279. static struct mb_sram_free_area *mb_sram_free_areas = &mb_sram_free_tail;
  280. /* The free-list of free free-list nodes. (:-) */
  281. static struct mb_sram_free_area *mb_sram_free_free_areas = 0;
  282. /* Spinlock protecting the above globals. */
  283. static DEFINE_SPINLOCK(mb_sram_lock);
  284. /* Allocate a memory block at least SIZE bytes long in the Mother-A SRAM
  285. space. */
  286. static void *alloc_mb_sram (size_t size)
  287. {
  288. struct mb_sram_free_area *prev, *fa;
  289. int flags;
  290. void *mem = 0;
  291. spin_lock_irqsave (mb_sram_lock, flags);
  292. /* Look for a free area that can contain SIZE bytes. */
  293. for (prev = 0, fa = mb_sram_free_areas; fa; prev = fa, fa = fa->next)
  294. if (fa->size >= size) {
  295. /* Found one! */
  296. mem = fa->mem;
  297. if (fa->size == size) {
  298. /* In fact, it fits exactly, so remove
  299. this node from the free-list. */
  300. if (prev)
  301. prev->next = fa->next;
  302. else
  303. mb_sram_free_areas = fa->next;
  304. /* Put it on the free-list-entry-free-list. */
  305. fa->next = mb_sram_free_free_areas;
  306. mb_sram_free_free_areas = fa;
  307. } else {
  308. /* FA is bigger than SIZE, so just
  309. reduce its size to account for this
  310. allocation. */
  311. fa->mem += size;
  312. fa->size -= size;
  313. }
  314. break;
  315. }
  316. spin_unlock_irqrestore (mb_sram_lock, flags);
  317. return mem;
  318. }
  319. /* Return the memory area MEM of size SIZE to the MB SRAM free pool. */
  320. static void free_mb_sram (void *mem, size_t size)
  321. {
  322. struct mb_sram_free_area *prev, *fa, *new_fa;
  323. int flags;
  324. void *end = mem + size;
  325. spin_lock_irqsave (mb_sram_lock, flags);
  326. retry:
  327. /* Find an adjacent free-list entry. */
  328. for (prev = 0, fa = mb_sram_free_areas; fa; prev = fa, fa = fa->next)
  329. if (fa->mem == end) {
  330. /* FA is just after MEM, grow down to encompass it. */
  331. fa->mem = mem;
  332. fa->size += size;
  333. goto done;
  334. } else if (fa->mem + fa->size == mem) {
  335. struct mb_sram_free_area *next_fa = fa->next;
  336. /* FA is just before MEM, expand to encompass it. */
  337. fa->size += size;
  338. /* See if FA can now be merged with its successor. */
  339. if (next_fa && fa->mem + fa->size == next_fa->mem) {
  340. /* Yup; merge NEXT_FA's info into FA. */
  341. fa->size += next_fa->size;
  342. fa->next = next_fa->next;
  343. /* Free NEXT_FA. */
  344. next_fa->next = mb_sram_free_free_areas;
  345. mb_sram_free_free_areas = next_fa;
  346. }
  347. goto done;
  348. } else if (fa->mem > mem)
  349. /* We've reached the right spot in the free-list
  350. without finding an adjacent free-area, so add
  351. a new free area to hold mem. */
  352. break;
  353. /* Make a new free-list entry. */
  354. /* First, get a free-list entry. */
  355. if (! mb_sram_free_free_areas) {
  356. /* There are none, so make some. */
  357. void *block;
  358. size_t block_size = sizeof (struct mb_sram_free_area) * 8;
  359. /* Don't hold the lock while calling kmalloc (I'm not
  360. sure whether it would be a problem, since we use
  361. GFP_ATOMIC, but it makes me nervous). */
  362. spin_unlock_irqrestore (mb_sram_lock, flags);
  363. block = kmalloc (block_size, GFP_ATOMIC);
  364. if (! block)
  365. panic ("free_mb_sram: can't allocate free-list entry");
  366. /* Now get the lock back. */
  367. spin_lock_irqsave (mb_sram_lock, flags);
  368. /* Add the new free free-list entries. */
  369. while (block_size > 0) {
  370. struct mb_sram_free_area *nfa = block;
  371. nfa->next = mb_sram_free_free_areas;
  372. mb_sram_free_free_areas = nfa;
  373. block += sizeof *nfa;
  374. block_size -= sizeof *nfa;
  375. }
  376. /* Since we dropped the lock to call kmalloc, the
  377. free-list could have changed, so retry from the
  378. beginning. */
  379. goto retry;
  380. }
  381. /* Remove NEW_FA from the free-list of free-list entries. */
  382. new_fa = mb_sram_free_free_areas;
  383. mb_sram_free_free_areas = new_fa->next;
  384. /* NEW_FA initially holds only MEM. */
  385. new_fa->mem = mem;
  386. new_fa->size = size;
  387. /* Insert NEW_FA in the free-list between PREV and FA. */
  388. new_fa->next = fa;
  389. if (prev)
  390. prev->next = new_fa;
  391. else
  392. mb_sram_free_areas = new_fa;
  393. done:
  394. spin_unlock_irqrestore (mb_sram_lock, flags);
  395. }
  396. /* Maintainence of CPU -> Mother-A DMA mappings. */
  397. struct dma_mapping {
  398. void *cpu_addr;
  399. void *mb_sram_addr;
  400. size_t size;
  401. struct dma_mapping *next;
  402. };
  403. /* A list of mappings from CPU addresses to MB SRAM addresses for active
  404. DMA blocks (that have been `granted' to the PCI device). */
  405. static struct dma_mapping *active_dma_mappings = 0;
  406. /* A list of free mapping objects. */
  407. static struct dma_mapping *free_dma_mappings = 0;
  408. /* Spinlock protecting the above globals. */
  409. static DEFINE_SPINLOCK(dma_mappings_lock);
  410. static struct dma_mapping *new_dma_mapping (size_t size)
  411. {
  412. int flags;
  413. struct dma_mapping *mapping;
  414. void *mb_sram_block = alloc_mb_sram (size);
  415. if (! mb_sram_block)
  416. return 0;
  417. spin_lock_irqsave (dma_mappings_lock, flags);
  418. if (! free_dma_mappings) {
  419. /* We're out of mapping structures, make more. */
  420. void *mblock;
  421. size_t mblock_size = sizeof (struct dma_mapping) * 8;
  422. /* Don't hold the lock while calling kmalloc (I'm not
  423. sure whether it would be a problem, since we use
  424. GFP_ATOMIC, but it makes me nervous). */
  425. spin_unlock_irqrestore (dma_mappings_lock, flags);
  426. mblock = kmalloc (mblock_size, GFP_ATOMIC);
  427. if (! mblock) {
  428. free_mb_sram (mb_sram_block, size);
  429. return 0;
  430. }
  431. /* Get the lock back. */
  432. spin_lock_irqsave (dma_mappings_lock, flags);
  433. /* Add the new mapping structures to the free-list. */
  434. while (mblock_size > 0) {
  435. struct dma_mapping *fm = mblock;
  436. fm->next = free_dma_mappings;
  437. free_dma_mappings = fm;
  438. mblock += sizeof *fm;
  439. mblock_size -= sizeof *fm;
  440. }
  441. }
  442. /* Get a mapping struct from the freelist. */
  443. mapping = free_dma_mappings;
  444. free_dma_mappings = mapping->next;
  445. /* Initialize the mapping. Other fields should be filled in by
  446. caller. */
  447. mapping->mb_sram_addr = mb_sram_block;
  448. mapping->size = size;
  449. /* Add it to the list of active mappings. */
  450. mapping->next = active_dma_mappings;
  451. active_dma_mappings = mapping;
  452. spin_unlock_irqrestore (dma_mappings_lock, flags);
  453. return mapping;
  454. }
  455. static struct dma_mapping *find_dma_mapping (void *mb_sram_addr)
  456. {
  457. int flags;
  458. struct dma_mapping *mapping;
  459. spin_lock_irqsave (dma_mappings_lock, flags);
  460. for (mapping = active_dma_mappings; mapping; mapping = mapping->next)
  461. if (mapping->mb_sram_addr == mb_sram_addr) {
  462. spin_unlock_irqrestore (dma_mappings_lock, flags);
  463. return mapping;
  464. }
  465. panic ("find_dma_mapping: unmapped PCI DMA addr 0x%x",
  466. MB_SRAM_TO_PCI (mb_sram_addr));
  467. }
  468. static struct dma_mapping *deactivate_dma_mapping (void *mb_sram_addr)
  469. {
  470. int flags;
  471. struct dma_mapping *mapping, *prev;
  472. spin_lock_irqsave (dma_mappings_lock, flags);
  473. for (prev = 0, mapping = active_dma_mappings;
  474. mapping;
  475. prev = mapping, mapping = mapping->next)
  476. {
  477. if (mapping->mb_sram_addr == mb_sram_addr) {
  478. /* This is the MAPPING; deactivate it. */
  479. if (prev)
  480. prev->next = mapping->next;
  481. else
  482. active_dma_mappings = mapping->next;
  483. spin_unlock_irqrestore (dma_mappings_lock, flags);
  484. return mapping;
  485. }
  486. }
  487. panic ("deactivate_dma_mapping: unmapped PCI DMA addr 0x%x",
  488. MB_SRAM_TO_PCI (mb_sram_addr));
  489. }
  490. /* Return MAPPING to the freelist. */
  491. static inline void
  492. free_dma_mapping (struct dma_mapping *mapping)
  493. {
  494. int flags;
  495. free_mb_sram (mapping->mb_sram_addr, mapping->size);
  496. spin_lock_irqsave (dma_mappings_lock, flags);
  497. mapping->next = free_dma_mappings;
  498. free_dma_mappings = mapping;
  499. spin_unlock_irqrestore (dma_mappings_lock, flags);
  500. }
  501. /* Single PCI DMA mappings. */
  502. /* `Grant' to PDEV the memory block at CPU_ADDR, for doing DMA. The
  503. 32-bit PCI bus mastering address to use is returned. the device owns
  504. this memory until either pci_unmap_single or pci_dma_sync_single is
  505. performed. */
  506. dma_addr_t
  507. pci_map_single (struct pci_dev *pdev, void *cpu_addr, size_t size, int dir)
  508. {
  509. struct dma_mapping *mapping = new_dma_mapping (size);
  510. if (! mapping)
  511. return 0;
  512. mapping->cpu_addr = cpu_addr;
  513. if (dir == PCI_DMA_BIDIRECTIONAL || dir == PCI_DMA_TODEVICE)
  514. memcpy (mapping->mb_sram_addr, cpu_addr, size);
  515. return MB_SRAM_TO_PCI (mapping->mb_sram_addr);
  516. }
  517. /* Return to the CPU the PCI DMA memory block previously `granted' to
  518. PDEV, at DMA_ADDR. */
  519. void pci_unmap_single (struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
  520. int dir)
  521. {
  522. void *mb_sram_addr = PCI_TO_MB_SRAM (dma_addr);
  523. struct dma_mapping *mapping = deactivate_dma_mapping (mb_sram_addr);
  524. if (size != mapping->size)
  525. panic ("pci_unmap_single: size (%d) doesn't match"
  526. " size of mapping at PCI DMA addr 0x%x (%d)\n",
  527. size, dma_addr, mapping->size);
  528. /* Copy back the DMA'd contents if necessary. */
  529. if (dir == PCI_DMA_BIDIRECTIONAL || dir == PCI_DMA_FROMDEVICE)
  530. memcpy (mapping->cpu_addr, mb_sram_addr, size);
  531. /* Return mapping to the freelist. */
  532. free_dma_mapping (mapping);
  533. }
  534. /* Make physical memory consistent for a single streaming mode DMA
  535. translation after a transfer.
  536. If you perform a pci_map_single() but wish to interrogate the
  537. buffer using the cpu, yet do not wish to teardown the PCI dma
  538. mapping, you must call this function before doing so. At the next
  539. point you give the PCI dma address back to the card, you must first
  540. perform a pci_dma_sync_for_device, and then the device again owns
  541. the buffer. */
  542. void
  543. pci_dma_sync_single_for_cpu (struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
  544. int dir)
  545. {
  546. void *mb_sram_addr = PCI_TO_MB_SRAM (dma_addr);
  547. struct dma_mapping *mapping = find_dma_mapping (mb_sram_addr);
  548. /* Synchronize the DMA buffer with the CPU buffer if necessary. */
  549. if (dir == PCI_DMA_FROMDEVICE)
  550. memcpy (mapping->cpu_addr, mb_sram_addr, size);
  551. else if (dir == PCI_DMA_TODEVICE)
  552. ; /* nothing to do */
  553. else
  554. panic("pci_dma_sync_single: unsupported sync dir: %d", dir);
  555. }
  556. void
  557. pci_dma_sync_single_for_device (struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
  558. int dir)
  559. {
  560. void *mb_sram_addr = PCI_TO_MB_SRAM (dma_addr);
  561. struct dma_mapping *mapping = find_dma_mapping (mb_sram_addr);
  562. /* Synchronize the DMA buffer with the CPU buffer if necessary. */
  563. if (dir == PCI_DMA_FROMDEVICE)
  564. ; /* nothing to do */
  565. else if (dir == PCI_DMA_TODEVICE)
  566. memcpy (mb_sram_addr, mapping->cpu_addr, size);
  567. else
  568. panic("pci_dma_sync_single: unsupported sync dir: %d", dir);
  569. }
  570. /* Scatter-gather PCI DMA mappings. */
  571. /* Do multiple DMA mappings at once. */
  572. int
  573. pci_map_sg (struct pci_dev *pdev, struct scatterlist *sg, int sg_len, int dir)
  574. {
  575. BUG ();
  576. return 0;
  577. }
  578. /* Unmap multiple DMA mappings at once. */
  579. void
  580. pci_unmap_sg (struct pci_dev *pdev, struct scatterlist *sg, int sg_len,int dir)
  581. {
  582. BUG ();
  583. }
  584. /* Make physical memory consistent for a set of streaming mode DMA
  585. translations after a transfer. The same as pci_dma_sync_single_* but
  586. for a scatter-gather list, same rules and usage. */
  587. void
  588. pci_dma_sync_sg_for_cpu (struct pci_dev *dev,
  589. struct scatterlist *sg, int sg_len,
  590. int dir)
  591. {
  592. BUG ();
  593. }
  594. void
  595. pci_dma_sync_sg_for_device (struct pci_dev *dev,
  596. struct scatterlist *sg, int sg_len,
  597. int dir)
  598. {
  599. BUG ();
  600. }
  601. /* PCI mem mapping. */
  602. /* Allocate and map kernel buffer using consistent mode DMA for PCI
  603. device. Returns non-NULL cpu-view pointer to the buffer if
  604. successful and sets *DMA_ADDR to the pci side dma address as well,
  605. else DMA_ADDR is undefined. */
  606. void *
  607. pci_alloc_consistent (struct pci_dev *pdev, size_t size, dma_addr_t *dma_addr)
  608. {
  609. void *mb_sram_mem = alloc_mb_sram (size);
  610. if (mb_sram_mem)
  611. *dma_addr = MB_SRAM_TO_PCI (mb_sram_mem);
  612. return mb_sram_mem;
  613. }
  614. /* Free and unmap a consistent DMA buffer. CPU_ADDR and DMA_ADDR must
  615. be values that were returned from pci_alloc_consistent. SIZE must be
  616. the same as what as passed into pci_alloc_consistent. References to
  617. the memory and mappings assosciated with CPU_ADDR or DMA_ADDR past
  618. this call are illegal. */
  619. void
  620. pci_free_consistent (struct pci_dev *pdev, size_t size, void *cpu_addr,
  621. dma_addr_t dma_addr)
  622. {
  623. void *mb_sram_mem = PCI_TO_MB_SRAM (dma_addr);
  624. free_mb_sram (mb_sram_mem, size);
  625. }
  626. /* iomap/iomap */
  627. void __iomem *pci_iomap (struct pci_dev *dev, int bar, unsigned long max)
  628. {
  629. unsigned long start = pci_resource_start (dev, bar);
  630. unsigned long len = pci_resource_len (dev, bar);
  631. if (!start || len == 0)
  632. return 0;
  633. /* None of the ioremap functions actually do anything, other than
  634. re-casting their argument, so don't bother differentiating them. */
  635. return ioremap (start, len);
  636. }
  637. void pci_iounmap (struct pci_dev *dev, void __iomem *addr)
  638. {
  639. /* nothing */
  640. }
  641. /* symbol exports (for modules) */
  642. EXPORT_SYMBOL (pci_map_single);
  643. EXPORT_SYMBOL (pci_unmap_single);
  644. EXPORT_SYMBOL (pci_alloc_consistent);
  645. EXPORT_SYMBOL (pci_free_consistent);
  646. EXPORT_SYMBOL (pci_dma_sync_single_for_cpu);
  647. EXPORT_SYMBOL (pci_dma_sync_single_for_device);
  648. EXPORT_SYMBOL (pci_iomap);
  649. EXPORT_SYMBOL (pci_iounmap);