init_ohci1394_dma.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * init_ohci1394_dma.c - Initializes physical DMA on all OHCI 1394 controllers
  3. *
  4. * Copyright (C) 2006-2007 Bernhard Kaindl <bk@suse.de>
  5. *
  6. * Derived from drivers/ieee1394/ohci1394.c and arch/x86/kernel/early-quirks.c
  7. * this file has functions to:
  8. * - scan the PCI very early on boot for all OHCI 1394-compliant controllers
  9. * - reset and initialize them and make them join the IEEE1394 bus and
  10. * - enable physical DMA on them to allow remote debugging
  11. *
  12. * All code and data is marked as __init and __initdata, respective as
  13. * during boot, all OHCI1394 controllers may be claimed by the firewire
  14. * stack and at this point, this code should not touch them anymore.
  15. *
  16. * To use physical DMA after the initialization of the firewire stack,
  17. * be sure that the stack enables it and (re-)attach after the bus reset
  18. * which may be caused by the firewire stack initialization.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software Foundation,
  32. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  33. */
  34. #include <linux/interrupt.h> /* for ohci1394.h */
  35. #include <linux/delay.h>
  36. #include <linux/pci.h> /* for PCI defines */
  37. #include <linux/init_ohci1394_dma.h>
  38. #include <asm/pci-direct.h> /* for direct PCI config space access */
  39. #include <asm/fixmap.h>
  40. #include "ieee1394_types.h"
  41. #include "ohci1394.h"
  42. int __initdata init_ohci1394_dma_early;
  43. /* Reads a PHY register of an OHCI-1394 controller */
  44. static inline u8 __init get_phy_reg(struct ti_ohci *ohci, u8 addr)
  45. {
  46. int i;
  47. quadlet_t r;
  48. reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | 0x00008000);
  49. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  50. if (reg_read(ohci, OHCI1394_PhyControl) & 0x80000000)
  51. break;
  52. mdelay(1);
  53. }
  54. r = reg_read(ohci, OHCI1394_PhyControl);
  55. return (r & 0x00ff0000) >> 16;
  56. }
  57. /* Writes to a PHY register of an OHCI-1394 controller */
  58. static inline void __init set_phy_reg(struct ti_ohci *ohci, u8 addr, u8 data)
  59. {
  60. int i;
  61. reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | data | 0x00004000);
  62. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  63. u32 r = reg_read(ohci, OHCI1394_PhyControl);
  64. if (!(r & 0x00004000))
  65. break;
  66. mdelay(1);
  67. }
  68. }
  69. /* Resets an OHCI-1394 controller (for sane state before initialization) */
  70. static inline void __init init_ohci1394_soft_reset(struct ti_ohci *ohci) {
  71. int i;
  72. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  73. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  74. if (!(reg_read(ohci, OHCI1394_HCControlSet)
  75. & OHCI1394_HCControl_softReset))
  76. break;
  77. mdelay(1);
  78. }
  79. }
  80. /* Basic OHCI-1394 register and port inititalization */
  81. static inline void __init init_ohci1394_initialize(struct ti_ohci *ohci)
  82. {
  83. quadlet_t bus_options;
  84. int num_ports, i;
  85. /* Put some defaults to these undefined bus options */
  86. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  87. bus_options |= 0x60000000; /* Enable CMC and ISC */
  88. bus_options &= ~0x00ff0000; /* XXX: Set cyc_clk_acc to zero for now */
  89. bus_options &= ~0x18000000; /* Disable PMC and BMC */
  90. reg_write(ohci, OHCI1394_BusOptions, bus_options);
  91. /* Set the bus number */
  92. reg_write(ohci, OHCI1394_NodeID, 0x0000ffc0);
  93. /* Enable posted writes */
  94. reg_write(ohci, OHCI1394_HCControlSet,
  95. OHCI1394_HCControl_postedWriteEnable);
  96. /* Clear link control register */
  97. reg_write(ohci, OHCI1394_LinkControlClear, 0xffffffff);
  98. /* enable phys */
  99. reg_write(ohci, OHCI1394_LinkControlSet,
  100. OHCI1394_LinkControl_RcvPhyPkt);
  101. /* Don't accept phy packets into AR request context */
  102. reg_write(ohci, OHCI1394_LinkControlClear, 0x00000400);
  103. /* Clear the Isochonouys interrupt masks */
  104. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 0xffffffff);
  105. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 0xffffffff);
  106. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 0xffffffff);
  107. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 0xffffffff);
  108. /* Accept asyncronous transfer requests from all nodes for now */
  109. reg_write(ohci,OHCI1394_AsReqFilterHiSet, 0x80000000);
  110. /* Specify asyncronous transfer retries */
  111. reg_write(ohci, OHCI1394_ATRetries,
  112. OHCI1394_MAX_AT_REQ_RETRIES |
  113. (OHCI1394_MAX_AT_RESP_RETRIES<<4) |
  114. (OHCI1394_MAX_PHYS_RESP_RETRIES<<8));
  115. /* We don't want hardware swapping */
  116. reg_write(ohci, OHCI1394_HCControlClear, OHCI1394_HCControl_noByteSwap);
  117. /* Enable link */
  118. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_linkEnable);
  119. /* If anything is connected to a port, make sure it is enabled */
  120. num_ports = get_phy_reg(ohci, 2) & 0xf;
  121. for (i = 0; i < num_ports; i++) {
  122. unsigned int status;
  123. set_phy_reg(ohci, 7, i);
  124. status = get_phy_reg(ohci, 8);
  125. if (status & 0x20)
  126. set_phy_reg(ohci, 8, status & ~1);
  127. }
  128. }
  129. /**
  130. * init_ohci1394_wait_for_busresets - wait until bus resets are completed
  131. *
  132. * OHCI1394 initialization itself and any device going on- or offline
  133. * and any cable issue cause a IEEE1394 bus reset. The OHCI1394 spec
  134. * specifies that physical DMA is disabled on each bus reset and it
  135. * has to be enabled after each bus reset when needed. We resort
  136. * to polling here because on early boot, we have no interrupts.
  137. */
  138. static inline void __init init_ohci1394_wait_for_busresets(struct ti_ohci *ohci)
  139. {
  140. int i, events;
  141. for (i=0; i < 9; i++) {
  142. mdelay(200);
  143. events = reg_read(ohci, OHCI1394_IntEventSet);
  144. if (events & OHCI1394_busReset)
  145. reg_write(ohci, OHCI1394_IntEventClear,
  146. OHCI1394_busReset);
  147. }
  148. }
  149. /**
  150. * init_ohci1394_enable_physical_dma - Enable physical DMA for remote debugging
  151. * This enables remote DMA access over IEEE1394 from every host for the low
  152. * 4GB of address space. DMA accesses above 4GB are not available currently.
  153. */
  154. static inline void __init init_ohci1394_enable_physical_dma(struct ti_ohci *hci)
  155. {
  156. reg_write(hci, OHCI1394_PhyReqFilterHiSet, 0xffffffff);
  157. reg_write(hci, OHCI1394_PhyReqFilterLoSet, 0xffffffff);
  158. reg_write(hci, OHCI1394_PhyUpperBound, 0xffff0000);
  159. }
  160. /**
  161. * init_ohci1394_reset_and_init_dma - init controller and enable DMA
  162. * This initializes the given controller and enables physical DMA engine in it.
  163. */
  164. static inline void __init init_ohci1394_reset_and_init_dma(struct ti_ohci *ohci)
  165. {
  166. /* Start off with a soft reset, clears everything to a sane state. */
  167. init_ohci1394_soft_reset(ohci);
  168. /* Accessing some registers without LPS enabled may cause lock up */
  169. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_LPS);
  170. /* Disable and clear interrupts */
  171. reg_write(ohci, OHCI1394_IntEventClear, 0xffffffff);
  172. reg_write(ohci, OHCI1394_IntMaskClear, 0xffffffff);
  173. mdelay(50); /* Wait 50msec to make sure we have full link enabled */
  174. init_ohci1394_initialize(ohci);
  175. /*
  176. * The initialization causes at least one IEEE1394 bus reset. Enabling
  177. * physical DMA only works *after* *all* bus resets have calmed down:
  178. */
  179. init_ohci1394_wait_for_busresets(ohci);
  180. /* We had to wait and do this now if we want to debug early problems */
  181. init_ohci1394_enable_physical_dma(ohci);
  182. }
  183. /**
  184. * init_ohci1394_controller - Map the registers of the controller and init DMA
  185. * This maps the registers of the specified controller and initializes it
  186. */
  187. static inline void __init init_ohci1394_controller(int num, int slot, int func)
  188. {
  189. unsigned long ohci_base;
  190. struct ti_ohci ohci;
  191. printk(KERN_INFO "init_ohci1394_dma: initializing OHCI-1394"
  192. " at %02x:%02x.%x\n", num, slot, func);
  193. ohci_base = read_pci_config(num, slot, func, PCI_BASE_ADDRESS_0+(0<<2))
  194. & PCI_BASE_ADDRESS_MEM_MASK;
  195. set_fixmap_nocache(FIX_OHCI1394_BASE, ohci_base);
  196. ohci.registers = (void *)fix_to_virt(FIX_OHCI1394_BASE);
  197. init_ohci1394_reset_and_init_dma(&ohci);
  198. }
  199. /**
  200. * debug_init_ohci1394_dma - scan for OHCI1394 controllers and init DMA on them
  201. * Scans the whole PCI space for OHCI1394 controllers and inits DMA on them
  202. */
  203. void __init init_ohci1394_dma_on_all_controllers(void)
  204. {
  205. int num, slot, func;
  206. if (!early_pci_allowed())
  207. return;
  208. /* Poor man's PCI discovery, the only thing we can do at early boot */
  209. for (num = 0; num < 32; num++) {
  210. for (slot = 0; slot < 32; slot++) {
  211. for (func = 0; func < 8; func++) {
  212. u32 class = read_pci_config(num,slot,func,
  213. PCI_CLASS_REVISION);
  214. if ((class == 0xffffffff))
  215. continue; /* No device at this func */
  216. if (class>>8 != PCI_CLASS_SERIAL_FIREWIRE_OHCI)
  217. continue; /* Not an OHCI-1394 device */
  218. init_ohci1394_controller(num, slot, func);
  219. break; /* Assume one controller per device */
  220. }
  221. }
  222. }
  223. printk(KERN_INFO "init_ohci1394_dma: finished initializing OHCI DMA\n");
  224. }
  225. /**
  226. * setup_init_ohci1394_early - enables early OHCI1394 DMA initialization
  227. */
  228. static int __init setup_ohci1394_dma(char *opt)
  229. {
  230. if (!strcmp(opt, "early"))
  231. init_ohci1394_dma_early = 1;
  232. return 0;
  233. }
  234. /* passing ohci1394_dma=early on boot causes early OHCI1394 DMA initialization */
  235. early_param("ohci1394_dma", setup_ohci1394_dma);