ohci.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /* ohci.c - OHCI Support. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/usb.h>
  22. #include <grub/usbtrans.h>
  23. #include <grub/misc.h>
  24. #include <grub/pci.h>
  25. #include <grub/cpu/pci.h>
  26. #include <grub/cpu/io.h>
  27. #include <grub/time.h>
  28. #include <grub/cs5536.h>
  29. #include <grub/loader.h>
  30. #include <grub/disk.h>
  31. GRUB_MOD_LICENSE ("GPLv3+");
  32. struct grub_ohci_hcca
  33. {
  34. /* Pointers to Interrupt Endpoint Descriptors. Not used by
  35. GRUB. */
  36. grub_uint32_t inttable[32];
  37. /* Current frame number. */
  38. grub_uint16_t framenumber;
  39. grub_uint16_t pad;
  40. /* List of completed TDs. */
  41. grub_uint32_t donehead;
  42. grub_uint8_t reserved[116];
  43. } GRUB_PACKED;
  44. /* OHCI General Transfer Descriptor */
  45. struct grub_ohci_td
  46. {
  47. /* Information used to construct the TOKEN packet. */
  48. grub_uint32_t token;
  49. grub_uint32_t buffer; /* LittleEndian physical address */
  50. grub_uint32_t next_td; /* LittleEndian physical address */
  51. grub_uint32_t buffer_end; /* LittleEndian physical address */
  52. /* next values are not for OHCI HW */
  53. volatile struct grub_ohci_td *link_td; /* pointer to next free/chained TD
  54. * pointer as uint32 */
  55. grub_uint32_t prev_td_phys; /* we need it to find previous TD
  56. * physical address in CPU endian */
  57. grub_uint32_t tr_index; /* index of TD in transfer */
  58. grub_uint8_t pad[8 - sizeof (volatile struct grub_ohci_td *)]; /* padding to 32 bytes */
  59. } GRUB_PACKED;
  60. /* OHCI Endpoint Descriptor. */
  61. struct grub_ohci_ed
  62. {
  63. grub_uint32_t target;
  64. grub_uint32_t td_tail;
  65. grub_uint32_t td_head;
  66. grub_uint32_t next_ed;
  67. } GRUB_PACKED;
  68. typedef volatile struct grub_ohci_td *grub_ohci_td_t;
  69. typedef volatile struct grub_ohci_ed *grub_ohci_ed_t;
  70. /* Experimental change of ED/TD allocation */
  71. /* Little bit similar as in UHCI */
  72. /* Implementation assumes:
  73. * 32-bits architecture - XXX: fix for 64-bits
  74. * memory allocated by grub_memalign_dma32 must be continuous
  75. * in virtual and also in physical memory */
  76. struct grub_ohci
  77. {
  78. volatile grub_uint32_t *iobase;
  79. volatile struct grub_ohci_hcca *hcca;
  80. grub_uint32_t hcca_addr;
  81. struct grub_pci_dma_chunk *hcca_chunk;
  82. grub_ohci_ed_t ed_ctrl; /* EDs for CONTROL */
  83. grub_uint32_t ed_ctrl_addr;
  84. struct grub_pci_dma_chunk *ed_ctrl_chunk;
  85. grub_ohci_ed_t ed_bulk; /* EDs for BULK */
  86. grub_uint32_t ed_bulk_addr;
  87. struct grub_pci_dma_chunk *ed_bulk_chunk;
  88. grub_ohci_td_t td; /* TDs */
  89. grub_uint32_t td_addr;
  90. struct grub_pci_dma_chunk *td_chunk;
  91. struct grub_ohci *next;
  92. grub_ohci_td_t td_free; /* Pointer to first free TD */
  93. };
  94. static struct grub_ohci *ohci;
  95. typedef enum
  96. {
  97. GRUB_OHCI_REG_REVISION = 0x00,
  98. GRUB_OHCI_REG_CONTROL,
  99. GRUB_OHCI_REG_CMDSTATUS,
  100. GRUB_OHCI_REG_INTSTATUS,
  101. GRUB_OHCI_REG_INTENA,
  102. GRUB_OHCI_REG_INTDIS,
  103. GRUB_OHCI_REG_HCCA,
  104. GRUB_OHCI_REG_PERIODIC,
  105. GRUB_OHCI_REG_CONTROLHEAD,
  106. GRUB_OHCI_REG_CONTROLCURR,
  107. GRUB_OHCI_REG_BULKHEAD,
  108. GRUB_OHCI_REG_BULKCURR,
  109. GRUB_OHCI_REG_DONEHEAD,
  110. GRUB_OHCI_REG_FRAME_INTERVAL,
  111. GRUB_OHCI_REG_PERIODIC_START = 16,
  112. GRUB_OHCI_REG_RHUBA = 18,
  113. GRUB_OHCI_REG_RHUBPORT = 21,
  114. GRUB_OHCI_REG_LEGACY_CONTROL = 0x100,
  115. GRUB_OHCI_REG_LEGACY_INPUT = 0x104,
  116. GRUB_OHCI_REG_LEGACY_OUTPUT = 0x108,
  117. GRUB_OHCI_REG_LEGACY_STATUS = 0x10c
  118. } grub_ohci_reg_t;
  119. #define GRUB_OHCI_RHUB_PORT_POWER_MASK 0x300
  120. #define GRUB_OHCI_RHUB_PORT_ALL_POWERED 0x200
  121. #define GRUB_OHCI_REG_FRAME_INTERVAL_FSMPS_MASK 0x8fff0000
  122. #define GRUB_OHCI_REG_FRAME_INTERVAL_FSMPS_SHIFT 16
  123. #define GRUB_OHCI_REG_FRAME_INTERVAL_FI_SHIFT 0
  124. /* XXX: Is this choice of timings sane? */
  125. #define GRUB_OHCI_FSMPS 0x2778
  126. #define GRUB_OHCI_PERIODIC_START 0x257f
  127. #define GRUB_OHCI_FRAME_INTERVAL 0x2edf
  128. #define GRUB_OHCI_SET_PORT_ENABLE (1 << 1)
  129. #define GRUB_OHCI_CLEAR_PORT_ENABLE (1 << 0)
  130. #define GRUB_OHCI_SET_PORT_RESET (1 << 4)
  131. #define GRUB_OHCI_SET_PORT_RESET_STATUS_CHANGE (1 << 20)
  132. #define GRUB_OHCI_REG_CONTROL_BULK_ENABLE (1 << 5)
  133. #define GRUB_OHCI_REG_CONTROL_CONTROL_ENABLE (1 << 4)
  134. #define GRUB_OHCI_RESET_CONNECT_CHANGE (1 << 16)
  135. #define GRUB_OHCI_CTRL_EDS 256
  136. #define GRUB_OHCI_BULK_EDS 510
  137. #define GRUB_OHCI_TDS 640
  138. #define GRUB_OHCI_ED_ADDR_MASK 0x7ff
  139. static inline grub_ohci_ed_t
  140. grub_ohci_ed_phys2virt (struct grub_ohci *o, int bulk, grub_uint32_t x)
  141. {
  142. if (!x)
  143. return NULL;
  144. if (bulk)
  145. return (grub_ohci_ed_t) (x - o->ed_bulk_addr
  146. + (grub_uint8_t *) o->ed_bulk);
  147. return (grub_ohci_ed_t) (x - o->ed_ctrl_addr
  148. + (grub_uint8_t *) o->ed_ctrl);
  149. }
  150. static grub_uint32_t
  151. grub_ohci_virt_to_phys (struct grub_ohci *o, int bulk, grub_ohci_ed_t x)
  152. {
  153. if (!x)
  154. return 0;
  155. if (bulk)
  156. return (grub_uint8_t *) x - (grub_uint8_t *) o->ed_bulk + o->ed_bulk_addr;
  157. return (grub_uint8_t *) x - (grub_uint8_t *) o->ed_ctrl + o->ed_ctrl_addr;
  158. }
  159. static inline grub_ohci_td_t
  160. grub_ohci_td_phys2virt (struct grub_ohci *o, grub_uint32_t x)
  161. {
  162. if (!x)
  163. return NULL;
  164. return (grub_ohci_td_t) (x - o->td_addr + (grub_uint8_t *) o->td);
  165. }
  166. static grub_uint32_t
  167. grub_ohci_td_virt2phys (struct grub_ohci *o, grub_ohci_td_t x)
  168. {
  169. if (!x)
  170. return 0;
  171. return (grub_uint8_t *)x - (grub_uint8_t *)o->td + o->td_addr;
  172. }
  173. static grub_uint32_t
  174. grub_ohci_readreg32 (struct grub_ohci *o, grub_ohci_reg_t reg)
  175. {
  176. return grub_le_to_cpu32 (*(o->iobase + reg));
  177. }
  178. static void
  179. grub_ohci_writereg32 (struct grub_ohci *o,
  180. grub_ohci_reg_t reg, grub_uint32_t val)
  181. {
  182. *(o->iobase + reg) = grub_cpu_to_le32 (val);
  183. }
  184. /* Iterate over all PCI devices. Determine if a device is an OHCI
  185. controller. If this is the case, initialize it. */
  186. static int
  187. grub_ohci_pci_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
  188. void *data __attribute__ ((unused)))
  189. {
  190. grub_uint32_t interf;
  191. grub_uint32_t base;
  192. grub_pci_address_t addr;
  193. struct grub_ohci *o;
  194. grub_uint32_t revision;
  195. int j;
  196. /* Determine IO base address. */
  197. grub_dprintf ("ohci", "pciid = %x\n", pciid);
  198. if (pciid == GRUB_CS5536_PCIID)
  199. {
  200. grub_uint64_t basereg;
  201. basereg = grub_cs5536_read_msr (dev, GRUB_CS5536_MSR_USB_OHCI_BASE);
  202. if (!(basereg & GRUB_CS5536_MSR_USB_BASE_MEMORY_ENABLE))
  203. {
  204. /* Shouldn't happen. */
  205. grub_dprintf ("ohci", "No OHCI address is assigned\n");
  206. return 0;
  207. }
  208. base = (basereg & GRUB_CS5536_MSR_USB_BASE_ADDR_MASK);
  209. basereg |= GRUB_CS5536_MSR_USB_BASE_BUS_MASTER;
  210. basereg &= ~GRUB_CS5536_MSR_USB_BASE_PME_ENABLED;
  211. basereg &= ~GRUB_CS5536_MSR_USB_BASE_PME_STATUS;
  212. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_USB_OHCI_BASE, basereg);
  213. }
  214. else
  215. {
  216. grub_uint32_t class_code;
  217. grub_uint32_t class;
  218. grub_uint32_t subclass;
  219. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  220. class_code = grub_pci_read (addr) >> 8;
  221. interf = class_code & 0xFF;
  222. subclass = (class_code >> 8) & 0xFF;
  223. class = class_code >> 16;
  224. /* If this is not an OHCI controller, just return. */
  225. if (class != 0x0c || subclass != 0x03 || interf != 0x10)
  226. return 0;
  227. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
  228. base = grub_pci_read (addr);
  229. base &= GRUB_PCI_ADDR_MEM_MASK;
  230. if (!base)
  231. {
  232. grub_dprintf ("ehci",
  233. "EHCI: EHCI is not mapper\n");
  234. return 0;
  235. }
  236. /* Set bus master - needed for coreboot, VMware, broken BIOSes etc. */
  237. addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  238. grub_pci_write_word(addr,
  239. GRUB_PCI_COMMAND_MEM_ENABLED
  240. | GRUB_PCI_COMMAND_BUS_MASTER
  241. | grub_pci_read_word(addr));
  242. grub_dprintf ("ohci", "class=0x%02x 0x%02x interface 0x%02x\n",
  243. class, subclass, interf);
  244. }
  245. /* Allocate memory for the controller and register it. */
  246. o = grub_malloc (sizeof (*o));
  247. if (! o)
  248. return 1;
  249. grub_memset ((void*)o, 0, sizeof (*o));
  250. o->iobase = grub_pci_device_map_range (dev, base, 0x800);
  251. grub_dprintf ("ohci", "base=%p\n", o->iobase);
  252. /* Reserve memory for the HCCA. */
  253. o->hcca_chunk = grub_memalign_dma32 (256, 256);
  254. if (! o->hcca_chunk)
  255. goto fail;
  256. o->hcca = grub_dma_get_virt (o->hcca_chunk);
  257. o->hcca_addr = grub_dma_get_phys (o->hcca_chunk);
  258. grub_memset ((void*)o->hcca, 0, sizeof(*o->hcca));
  259. grub_dprintf ("ohci", "hcca: chunk=%p, virt=%p, phys=0x%02x\n",
  260. o->hcca_chunk, o->hcca, o->hcca_addr);
  261. /* Reserve memory for ctrl EDs. */
  262. o->ed_ctrl_chunk = grub_memalign_dma32 (16, sizeof(struct grub_ohci_ed)
  263. * GRUB_OHCI_CTRL_EDS);
  264. if (! o->ed_ctrl_chunk)
  265. goto fail;
  266. o->ed_ctrl = grub_dma_get_virt (o->ed_ctrl_chunk);
  267. o->ed_ctrl_addr = grub_dma_get_phys (o->ed_ctrl_chunk);
  268. /* Preset EDs */
  269. grub_memset ((void *) o->ed_ctrl, 0, sizeof (struct grub_ohci_ed)
  270. * GRUB_OHCI_CTRL_EDS);
  271. for (j=0; j < GRUB_OHCI_CTRL_EDS; j++)
  272. o->ed_ctrl[j].target = grub_cpu_to_le32_compile_time (1 << 14); /* skip */
  273. grub_dprintf ("ohci", "EDs-C: chunk=%p, virt=%p, phys=0x%02x\n",
  274. o->ed_ctrl_chunk, o->ed_ctrl, o->ed_ctrl_addr);
  275. /* Reserve memory for bulk EDs. */
  276. o->ed_bulk_chunk = grub_memalign_dma32 (16, sizeof (struct grub_ohci_ed)
  277. * GRUB_OHCI_BULK_EDS);
  278. if (! o->ed_bulk_chunk)
  279. goto fail;
  280. o->ed_bulk = grub_dma_get_virt (o->ed_bulk_chunk);
  281. o->ed_bulk_addr = grub_dma_get_phys (o->ed_bulk_chunk);
  282. /* Preset EDs */
  283. grub_memset ((void*)o->ed_bulk, 0, sizeof(struct grub_ohci_ed) * GRUB_OHCI_BULK_EDS);
  284. for (j=0; j < GRUB_OHCI_BULK_EDS; j++)
  285. o->ed_bulk[j].target = grub_cpu_to_le32_compile_time (1 << 14); /* skip */
  286. grub_dprintf ("ohci", "EDs-B: chunk=%p, virt=%p, phys=0x%02x\n",
  287. o->ed_bulk_chunk, o->ed_bulk, o->ed_bulk_addr);
  288. /* Reserve memory for TDs. */
  289. o->td_chunk = grub_memalign_dma32 (32, sizeof(struct grub_ohci_td)*GRUB_OHCI_TDS);
  290. /* Why is it aligned on 32 boundary if spec. says 16 ?
  291. * We have structure 32 bytes long and we don't want cross
  292. * 4K boundary inside structure. */
  293. if (! o->td_chunk)
  294. goto fail;
  295. o->td_free = o->td = grub_dma_get_virt (o->td_chunk);
  296. o->td_addr = grub_dma_get_phys (o->td_chunk);
  297. /* Preset free TDs chain in TDs */
  298. grub_memset ((void*)o->td, 0, sizeof(struct grub_ohci_td) * GRUB_OHCI_TDS);
  299. for (j=0; j < (GRUB_OHCI_TDS-1); j++)
  300. o->td[j].link_td = &o->td[j+1];
  301. grub_dprintf ("ohci", "TDs: chunk=%p, virt=%p, phys=0x%02x\n",
  302. o->td_chunk, o->td, o->td_addr);
  303. /* Check if the OHCI revision is actually 1.0 as supported. */
  304. revision = grub_ohci_readreg32 (o, GRUB_OHCI_REG_REVISION);
  305. grub_dprintf ("ohci", "OHCI revision=0x%02x\n", revision & 0xFF);
  306. if ((revision & 0xFF) != 0x10)
  307. goto fail;
  308. {
  309. grub_uint32_t control;
  310. /* Check SMM/BIOS ownership of OHCI (SMM = USB Legacy Support driver for BIOS) */
  311. control = grub_ohci_readreg32 (o, GRUB_OHCI_REG_CONTROL);
  312. if ((control & 0x100) != 0)
  313. {
  314. unsigned i;
  315. grub_dprintf("ohci", "OHCI is owned by SMM\n");
  316. /* Do change of ownership */
  317. /* Ownership change request */
  318. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, (1<<3)); /* XXX: Magic. */
  319. /* Waiting for SMM deactivation */
  320. for (i=0; i < 10; i++)
  321. {
  322. if ((grub_ohci_readreg32 (o, GRUB_OHCI_REG_CONTROL) & 0x100) == 0)
  323. {
  324. grub_dprintf("ohci", "Ownership changed normally.\n");
  325. break;
  326. }
  327. grub_millisleep (100);
  328. }
  329. if (i >= 10)
  330. {
  331. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL,
  332. grub_ohci_readreg32 (o, GRUB_OHCI_REG_CONTROL) & ~0x100);
  333. grub_dprintf("ohci", "Ownership changing timeout, change forced !\n");
  334. }
  335. }
  336. else if (((control & 0x100) == 0) &&
  337. ((control & 0xc0) != 0)) /* Not owned by SMM nor reset */
  338. {
  339. grub_dprintf("ohci", "OHCI is owned by BIOS\n");
  340. /* Do change of ownership - not implemented yet... */
  341. /* In fact we probably need to do nothing ...? */
  342. }
  343. else
  344. {
  345. grub_dprintf("ohci", "OHCI is not owned by SMM nor BIOS\n");
  346. /* We can setup OHCI. */
  347. }
  348. }
  349. /* Suspend the OHCI by issuing a reset. */
  350. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, 1); /* XXX: Magic. */
  351. grub_millisleep (1);
  352. grub_dprintf ("ohci", "OHCI reset\n");
  353. grub_ohci_writereg32 (o, GRUB_OHCI_REG_FRAME_INTERVAL,
  354. (GRUB_OHCI_FSMPS
  355. << GRUB_OHCI_REG_FRAME_INTERVAL_FSMPS_SHIFT)
  356. | (GRUB_OHCI_FRAME_INTERVAL
  357. << GRUB_OHCI_REG_FRAME_INTERVAL_FI_SHIFT));
  358. grub_ohci_writereg32 (o, GRUB_OHCI_REG_PERIODIC_START,
  359. GRUB_OHCI_PERIODIC_START);
  360. /* Setup the HCCA. */
  361. o->hcca->donehead = 0;
  362. grub_ohci_writereg32 (o, GRUB_OHCI_REG_HCCA, o->hcca_addr);
  363. grub_dprintf ("ohci", "OHCI HCCA\n");
  364. /* Misc. pre-sets. */
  365. o->hcca->donehead = 0;
  366. grub_ohci_writereg32 (o, GRUB_OHCI_REG_INTSTATUS, 0x7f); /* Clears everything */
  367. /* We don't want modify CONTROL/BULK HEAD registers.
  368. * So we assign to HEAD registers zero ED from related array
  369. * and we will not use this ED, it will be always skipped.
  370. * It should not produce notable performance penalty (I hope). */
  371. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLHEAD, o->ed_ctrl_addr);
  372. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLCURR, 0);
  373. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKHEAD, o->ed_bulk_addr);
  374. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKCURR, 0);
  375. /* Check OHCI Legacy Support */
  376. if ((revision & 0x100) != 0)
  377. {
  378. grub_dprintf ("ohci", "Legacy Support registers detected\n");
  379. grub_dprintf ("ohci", "Current state of legacy control reg.: 0x%04x\n",
  380. grub_ohci_readreg32 (o, GRUB_OHCI_REG_LEGACY_CONTROL));
  381. grub_ohci_writereg32 (o, GRUB_OHCI_REG_LEGACY_CONTROL,
  382. (grub_ohci_readreg32 (o, GRUB_OHCI_REG_LEGACY_CONTROL)) & ~1);
  383. grub_dprintf ("ohci", "OHCI Legacy Support disabled.\n");
  384. }
  385. /* Enable the OHCI + enable CONTROL and BULK LIST. */
  386. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL,
  387. (2 << 6)
  388. | GRUB_OHCI_REG_CONTROL_CONTROL_ENABLE
  389. | GRUB_OHCI_REG_CONTROL_BULK_ENABLE );
  390. grub_dprintf ("ohci", "OHCI enable: 0x%02x\n",
  391. (grub_ohci_readreg32 (o, GRUB_OHCI_REG_CONTROL) >> 6) & 3);
  392. /* Power on all ports */
  393. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBA,
  394. (grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBA)
  395. & ~GRUB_OHCI_RHUB_PORT_POWER_MASK)
  396. | GRUB_OHCI_RHUB_PORT_ALL_POWERED);
  397. #if 0 /* We don't need it at all, handled via hotplugging */
  398. /* Now we have hot-plugging, we need to wait for stable power only */
  399. grub_millisleep (100);
  400. #endif
  401. /* Link to ohci now that initialisation is successful. */
  402. o->next = ohci;
  403. ohci = o;
  404. return 0;
  405. fail:
  406. if (o)
  407. {
  408. grub_dma_free (o->td_chunk);
  409. grub_dma_free (o->ed_bulk_chunk);
  410. grub_dma_free (o->ed_ctrl_chunk);
  411. grub_dma_free (o->hcca_chunk);
  412. }
  413. grub_free (o);
  414. return 0;
  415. }
  416. static void
  417. grub_ohci_inithw (void)
  418. {
  419. grub_pci_iterate (grub_ohci_pci_iter, NULL);
  420. }
  421. static int
  422. grub_ohci_iterate (grub_usb_controller_iterate_hook_t hook, void *hook_data)
  423. {
  424. struct grub_ohci *o;
  425. struct grub_usb_controller dev;
  426. for (o = ohci; o; o = o->next)
  427. {
  428. dev.data = o;
  429. if (hook (&dev, hook_data))
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. static grub_ohci_ed_t
  435. grub_ohci_find_ed (struct grub_ohci *o, int bulk, grub_uint32_t target)
  436. {
  437. grub_ohci_ed_t ed, ed_next;
  438. grub_uint32_t target_addr = target & GRUB_OHCI_ED_ADDR_MASK;
  439. int count;
  440. int i;
  441. /* Use proper values and structures. */
  442. if (bulk)
  443. {
  444. count = GRUB_OHCI_BULK_EDS;
  445. ed = o->ed_bulk;
  446. ed_next = grub_ohci_ed_phys2virt(o, bulk,
  447. grub_le_to_cpu32 (ed->next_ed) );
  448. }
  449. else
  450. {
  451. count = GRUB_OHCI_CTRL_EDS;
  452. ed = o->ed_ctrl;
  453. ed_next = grub_ohci_ed_phys2virt(o, bulk,
  454. grub_le_to_cpu32 (ed->next_ed) );
  455. }
  456. /* First try to find existing ED with proper target address */
  457. for (i = 0; ; )
  458. {
  459. if (i && /* We ignore zero ED */
  460. ((ed->target & GRUB_OHCI_ED_ADDR_MASK) == target_addr))
  461. return ed; /* Found proper existing ED */
  462. i++;
  463. if (ed_next && (i < count))
  464. {
  465. ed = ed_next;
  466. ed_next = grub_ohci_ed_phys2virt(o, bulk,
  467. grub_le_to_cpu32 (ed->next_ed) );
  468. continue;
  469. }
  470. break;
  471. }
  472. /* ED with target_addr does not exist, we have to add it */
  473. /* Have we any free ED in array ? */
  474. if (i >= count) /* No. */
  475. return NULL;
  476. /* Currently we simply take next ED in array, no allocation
  477. * function is used. It should be no problem until hot-plugging
  478. * will be implemented, i.e. until we will need to de-allocate EDs
  479. * of unplugged devices. */
  480. /* We can link new ED to previous ED safely as the new ED should
  481. * still have set skip bit. */
  482. ed->next_ed = grub_cpu_to_le32 ( grub_ohci_virt_to_phys (o,
  483. bulk, &ed[1]));
  484. return &ed[1];
  485. }
  486. static grub_ohci_td_t
  487. grub_ohci_alloc_td (struct grub_ohci *o)
  488. {
  489. grub_ohci_td_t ret;
  490. /* Check if there is a Transfer Descriptor available. */
  491. if (! o->td_free)
  492. return NULL;
  493. ret = o->td_free; /* Take current free TD */
  494. o->td_free = (grub_ohci_td_t)ret->link_td; /* Advance to next free TD in chain */
  495. ret->link_td = 0; /* Reset link_td in allocated TD */
  496. return ret;
  497. }
  498. static void
  499. grub_ohci_free_td (struct grub_ohci *o, grub_ohci_td_t td)
  500. {
  501. grub_memset ( (void*)td, 0, sizeof(struct grub_ohci_td) );
  502. td->link_td = o->td_free; /* Cahin new free TD & rest */
  503. o->td_free = td; /* Change address of first free TD */
  504. }
  505. static void
  506. grub_ohci_free_tds (struct grub_ohci *o, grub_ohci_td_t td)
  507. {
  508. if (!td)
  509. return;
  510. /* Unchain first TD from previous TD if it is chained */
  511. if (td->prev_td_phys)
  512. {
  513. grub_ohci_td_t td_prev_virt = grub_ohci_td_phys2virt(o,
  514. td->prev_td_phys);
  515. if (td == (grub_ohci_td_t) td_prev_virt->link_td)
  516. td_prev_virt->link_td = 0;
  517. }
  518. /* Free all TDs from td (chained by link_td) */
  519. while (td)
  520. {
  521. grub_ohci_td_t tdprev;
  522. /* Unlink the queue. */
  523. tdprev = td;
  524. td = (grub_ohci_td_t) td->link_td;
  525. /* Free the TD. */
  526. grub_ohci_free_td (o, tdprev);
  527. }
  528. }
  529. static void
  530. grub_ohci_transaction (grub_ohci_td_t td,
  531. grub_transfer_type_t type, unsigned int toggle,
  532. grub_size_t size, grub_uint32_t data)
  533. {
  534. grub_uint32_t token;
  535. grub_uint32_t buffer;
  536. grub_uint32_t buffer_end;
  537. grub_dprintf ("ohci", "OHCI transaction td=%p type=%d, toggle=%d, size=%lu\n",
  538. td, type, toggle, (unsigned long) size);
  539. switch (type)
  540. {
  541. case GRUB_USB_TRANSFER_TYPE_SETUP:
  542. token = 0 << 19;
  543. break;
  544. case GRUB_USB_TRANSFER_TYPE_IN:
  545. token = 2 << 19;
  546. break;
  547. case GRUB_USB_TRANSFER_TYPE_OUT:
  548. token = 1 << 19;
  549. break;
  550. default:
  551. token = 0;
  552. break;
  553. }
  554. /* Set the token */
  555. token |= ( 7 << 21); /* Never generate interrupt */
  556. token |= toggle << 24;
  557. token |= 1 << 25;
  558. /* Set "Not accessed" error code */
  559. token |= 15 << 28;
  560. buffer = data;
  561. buffer_end = buffer + size - 1;
  562. /* Set correct buffer values in TD if zero transfer occurs */
  563. if (size)
  564. {
  565. buffer = (grub_uint32_t) data;
  566. buffer_end = buffer + size - 1;
  567. td->buffer = grub_cpu_to_le32 (buffer);
  568. td->buffer_end = grub_cpu_to_le32 (buffer_end);
  569. }
  570. else
  571. {
  572. td->buffer = 0;
  573. td->buffer_end = 0;
  574. }
  575. /* Set the rest of TD */
  576. td->token = grub_cpu_to_le32 (token);
  577. td->next_td = 0;
  578. }
  579. struct grub_ohci_transfer_controller_data
  580. {
  581. grub_uint32_t tderr_phys;
  582. grub_uint32_t td_last_phys;
  583. grub_ohci_ed_t ed_virt;
  584. grub_ohci_td_t td_current_virt;
  585. grub_ohci_td_t td_head_virt;
  586. };
  587. static grub_usb_err_t
  588. grub_ohci_setup_transfer (grub_usb_controller_t dev,
  589. grub_usb_transfer_t transfer)
  590. {
  591. struct grub_ohci *o = (struct grub_ohci *) dev->data;
  592. int bulk = 0;
  593. grub_ohci_td_t td_next_virt;
  594. grub_uint32_t target;
  595. grub_uint32_t td_head_phys;
  596. grub_uint32_t td_tail_phys;
  597. int i;
  598. struct grub_ohci_transfer_controller_data *cdata;
  599. cdata = grub_zalloc (sizeof (*cdata));
  600. if (!cdata)
  601. return GRUB_USB_ERR_INTERNAL;
  602. /* Pre-set target for ED - we need it to find proper ED */
  603. /* Set the device address. */
  604. target = transfer->devaddr;
  605. /* Set the endpoint. It should be masked, we need 4 bits only. */
  606. target |= (transfer->endpoint & 15) << 7;
  607. /* Set the device speed. */
  608. target |= (transfer->dev->speed == GRUB_USB_SPEED_LOW) << 13;
  609. /* Set the maximum packet size. */
  610. target |= transfer->max << 16;
  611. /* Determine if transfer type is bulk - we need to select proper ED */
  612. switch (transfer->type)
  613. {
  614. case GRUB_USB_TRANSACTION_TYPE_BULK:
  615. bulk = 1;
  616. break;
  617. case GRUB_USB_TRANSACTION_TYPE_CONTROL:
  618. break;
  619. default:
  620. grub_free (cdata);
  621. return GRUB_USB_ERR_INTERNAL;
  622. }
  623. /* Find proper ED or add new ED */
  624. cdata->ed_virt = grub_ohci_find_ed (o, bulk, target);
  625. if (!cdata->ed_virt)
  626. {
  627. grub_dprintf ("ohci","Fatal: No free ED !\n");
  628. grub_free (cdata);
  629. return GRUB_USB_ERR_INTERNAL;
  630. }
  631. /* Take pointer to first TD from ED */
  632. td_head_phys = grub_le_to_cpu32 (cdata->ed_virt->td_head) & ~0xf;
  633. td_tail_phys = grub_le_to_cpu32 (cdata->ed_virt->td_tail) & ~0xf;
  634. /* Sanity check - td_head should be equal to td_tail */
  635. if (td_head_phys != td_tail_phys) /* Should never happen ! */
  636. {
  637. grub_dprintf ("ohci", "Fatal: HEAD is not equal to TAIL !\n");
  638. grub_dprintf ("ohci", "HEAD = 0x%02x, TAIL = 0x%02x\n",
  639. td_head_phys, td_tail_phys);
  640. /* XXX: Fix: What to do ? */
  641. grub_free (cdata);
  642. return GRUB_USB_ERR_INTERNAL;
  643. }
  644. /* Now we should handle first TD. If ED is newly allocated,
  645. * we must allocate the first TD. */
  646. if (!td_head_phys)
  647. {
  648. cdata->td_head_virt = grub_ohci_alloc_td (o);
  649. if (!cdata->td_head_virt)
  650. {
  651. grub_free (cdata);
  652. return GRUB_USB_ERR_INTERNAL; /* We don't need de-allocate ED */
  653. }
  654. /* We can set td_head only when ED is not active, i.e.
  655. * when it is newly allocated. */
  656. cdata->ed_virt->td_head
  657. = grub_cpu_to_le32 (grub_ohci_td_virt2phys (o, cdata->td_head_virt));
  658. cdata->ed_virt->td_tail = cdata->ed_virt->td_head;
  659. }
  660. else
  661. cdata->td_head_virt = grub_ohci_td_phys2virt ( o, td_head_phys );
  662. /* Set TDs */
  663. cdata->td_last_phys = td_head_phys; /* initial value to make compiler happy... */
  664. for (i = 0, cdata->td_current_virt = cdata->td_head_virt;
  665. i < transfer->transcnt; i++)
  666. {
  667. grub_usb_transaction_t tr = &transfer->transactions[i];
  668. grub_ohci_transaction (cdata->td_current_virt, tr->pid, tr->toggle,
  669. tr->size, tr->data);
  670. /* Set index of TD in transfer */
  671. cdata->td_current_virt->tr_index = (grub_uint32_t) i;
  672. /* Remember last used (processed) TD phys. addr. */
  673. cdata->td_last_phys = grub_ohci_td_virt2phys (o, cdata->td_current_virt);
  674. /* Allocate next TD */
  675. td_next_virt = grub_ohci_alloc_td (o);
  676. if (!td_next_virt) /* No free TD, cancel transfer and free TDs except head TD */
  677. {
  678. if (i) /* if i==0 we have nothing to free... */
  679. grub_ohci_free_tds (o, grub_ohci_td_phys2virt(o,
  680. grub_le_to_cpu32 (cdata->td_head_virt->next_td)));
  681. /* Reset head TD */
  682. grub_memset ( (void*)cdata->td_head_virt, 0,
  683. sizeof(struct grub_ohci_td) );
  684. grub_dprintf ("ohci", "Fatal: No free TD !");
  685. grub_free (cdata);
  686. return GRUB_USB_ERR_INTERNAL;
  687. }
  688. /* Chain TDs */
  689. cdata->td_current_virt->link_td = td_next_virt;
  690. cdata->td_current_virt->next_td = grub_cpu_to_le32 (
  691. grub_ohci_td_virt2phys (o,
  692. td_next_virt) );
  693. td_next_virt->prev_td_phys = grub_ohci_td_virt2phys (o,
  694. cdata->td_current_virt);
  695. cdata->td_current_virt = td_next_virt;
  696. }
  697. grub_dprintf ("ohci", "Tail TD (not processed) = %p\n",
  698. cdata->td_current_virt);
  699. /* Setup the Endpoint Descriptor for transfer. */
  700. /* First set necessary fields in TARGET but keep (or set) skip bit */
  701. /* Note: It could be simpler if speed, format and max. packet
  702. * size never change after first allocation of ED.
  703. * But unfortunately max. packet size may change during initial
  704. * setup sequence and we must handle it. */
  705. cdata->ed_virt->target = grub_cpu_to_le32 (target | (1 << 14));
  706. /* Set td_tail */
  707. cdata->ed_virt->td_tail
  708. = grub_cpu_to_le32 (grub_ohci_td_virt2phys (o, cdata->td_current_virt));
  709. /* Now reset skip bit */
  710. cdata->ed_virt->target = grub_cpu_to_le32 (target);
  711. /* ed_virt->td_head = grub_cpu_to_le32 (td_head); Must not be changed, it is maintained by OHCI */
  712. /* ed_virt->next_ed = grub_cpu_to_le32 (0); Handled by grub_ohci_find_ed, do not change ! */
  713. grub_dprintf ("ohci", "program OHCI\n");
  714. /* Program the OHCI to actually transfer. */
  715. switch (transfer->type)
  716. {
  717. case GRUB_USB_TRANSACTION_TYPE_BULK:
  718. {
  719. grub_dprintf ("ohci", "BULK list filled\n");
  720. /* Set BulkListFilled. */
  721. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, 1 << 2);
  722. /* Read back of register should ensure it is really written */
  723. grub_ohci_readreg32 (o, GRUB_OHCI_REG_CMDSTATUS);
  724. break;
  725. }
  726. case GRUB_USB_TRANSACTION_TYPE_CONTROL:
  727. {
  728. grub_dprintf ("ohci", "CONTROL list filled\n");
  729. /* Set ControlListFilled. */
  730. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, 1 << 1);
  731. /* Read back of register should ensure it is really written */
  732. grub_ohci_readreg32 (o, GRUB_OHCI_REG_CMDSTATUS);
  733. break;
  734. }
  735. }
  736. transfer->controller_data = cdata;
  737. return GRUB_USB_ERR_NONE;
  738. }
  739. static void
  740. pre_finish_transfer (grub_usb_controller_t dev,
  741. grub_usb_transfer_t transfer)
  742. {
  743. struct grub_ohci *o = dev->data;
  744. struct grub_ohci_transfer_controller_data *cdata = transfer->controller_data;
  745. grub_uint32_t target;
  746. grub_uint32_t status;
  747. grub_uint32_t control;
  748. grub_uint32_t intstatus;
  749. /* There are many ways how the loop above can finish:
  750. * - normally without any error via INTSTATUS WDH bit
  751. * : tderr_phys == td_last_phys, td_head == td_tail
  752. * - normally with error via HALT bit in ED TD HEAD
  753. * : td_head = next TD after TD with error
  754. * : tderr_phys = last processed and retired TD with error,
  755. * i.e. should be != 0
  756. * : if bad_OHCI == TRUE, tderr_phys will be probably invalid
  757. * - unrecoverable error - I never seen it but it could be
  758. * : err_unrec == TRUE, other values can contain anything...
  759. * - timeout, it can be caused by:
  760. * -- bad USB device - some devices have some bugs, see Linux source
  761. * and related links
  762. * -- bad OHCI controller - e.g. lost interrupts or does not set
  763. * proper bits in INTSTATUS when real IRQ not enabled etc.,
  764. * see Linux source and related links
  765. * One known bug is handled - if transfer finished
  766. * successfully (i.e. HEAD==TAIL, last transfer TD is retired,
  767. * HALT bit is not set) and WDH bit is not set in INTSTATUS - in
  768. * this case we set o->bad_OHCI=TRUE and do alternate loop
  769. * and error handling - but there is problem how to find retired
  770. * TD with error code if HALT occurs and if DONEHEAD is not
  771. * working - we need to find TD previous to current ED HEAD
  772. * -- bad code of this driver or some unknown reasons - :-(
  773. * it can be e.g. bad handling of EDs/TDs/toggle bit...
  774. */
  775. /* Remember target for debug and set skip flag in ED */
  776. /* It should be normaly not necessary but we need it at least
  777. * in case of timeout */
  778. target = grub_le_to_cpu32 ( cdata->ed_virt->target );
  779. cdata->ed_virt->target = grub_cpu_to_le32 (target | (1 << 14));
  780. /* Read registers for debug - they should be read now because
  781. * debug prints case unwanted delays, so something can happen
  782. * in the meantime... */
  783. control = grub_ohci_readreg32 (o, GRUB_OHCI_REG_CONTROL);
  784. status = grub_ohci_readreg32 (o, GRUB_OHCI_REG_CMDSTATUS);
  785. intstatus = grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS);
  786. /* Now print debug values - to have full info what happened */
  787. grub_dprintf ("ohci", "loop finished: control=0x%02x status=0x%02x\n",
  788. control, status);
  789. grub_dprintf ("ohci", "intstatus=0x%02x, td_last_phys=0x%02x\n",
  790. intstatus, cdata->td_last_phys);
  791. grub_dprintf ("ohci", "TARGET=0x%02x, HEAD=0x%02x, TAIL=0x%02x\n",
  792. target,
  793. grub_le_to_cpu32 (cdata->ed_virt->td_head),
  794. grub_le_to_cpu32 (cdata->ed_virt->td_tail) );
  795. }
  796. static void
  797. finish_transfer (grub_usb_controller_t dev,
  798. grub_usb_transfer_t transfer)
  799. {
  800. struct grub_ohci *o = dev->data;
  801. struct grub_ohci_transfer_controller_data *cdata = transfer->controller_data;
  802. /* Set empty ED - set HEAD = TAIL = last (not processed) TD */
  803. cdata->ed_virt->td_head = grub_cpu_to_le32 (grub_le_to_cpu32 (cdata->ed_virt->td_tail) & ~0xf);
  804. /* At this point always should be:
  805. * ED has skip bit set and halted or empty or after next SOF,
  806. * i.e. it is safe to free all TDs except last not processed
  807. * ED HEAD == TAIL == phys. addr. of td_current_virt */
  808. /* Un-chainig of last TD */
  809. if (cdata->td_current_virt->prev_td_phys)
  810. {
  811. grub_ohci_td_t td_prev_virt
  812. = grub_ohci_td_phys2virt (o, cdata->td_current_virt->prev_td_phys);
  813. if (cdata->td_current_virt == (grub_ohci_td_t) td_prev_virt->link_td)
  814. td_prev_virt->link_td = 0;
  815. cdata->td_current_virt->prev_td_phys = 0;
  816. }
  817. grub_dprintf ("ohci", "OHCI finished, freeing\n");
  818. grub_ohci_free_tds (o, cdata->td_head_virt);
  819. grub_free (cdata);
  820. }
  821. static grub_usb_err_t
  822. parse_halt (grub_usb_controller_t dev,
  823. grub_usb_transfer_t transfer,
  824. grub_size_t *actual)
  825. {
  826. struct grub_ohci *o = dev->data;
  827. struct grub_ohci_transfer_controller_data *cdata = transfer->controller_data;
  828. grub_uint8_t errcode = 0;
  829. grub_usb_err_t err = GRUB_USB_ERR_NAK;
  830. grub_ohci_td_t tderr_virt = NULL;
  831. *actual = 0;
  832. pre_finish_transfer (dev, transfer);
  833. /* First we must get proper tderr_phys value */
  834. /* Retired TD with error should be previous TD to ED->td_head */
  835. cdata->tderr_phys = grub_ohci_td_phys2virt (o,
  836. grub_le_to_cpu32 (cdata->ed_virt->td_head) & ~0xf )
  837. ->prev_td_phys;
  838. /* Prepare pointer to last processed TD and get error code */
  839. tderr_virt = grub_ohci_td_phys2virt (o, cdata->tderr_phys);
  840. /* Set index of last processed TD */
  841. if (tderr_virt)
  842. {
  843. errcode = grub_le_to_cpu32 (tderr_virt->token) >> 28;
  844. transfer->last_trans = tderr_virt->tr_index;
  845. }
  846. else
  847. transfer->last_trans = -1;
  848. /* Evaluation of error code */
  849. grub_dprintf ("ohci", "OHCI tderr_phys=0x%02x, errcode=0x%02x\n",
  850. cdata->tderr_phys, errcode);
  851. switch (errcode)
  852. {
  853. case 0:
  854. /* XXX: Should not happen! */
  855. grub_error (GRUB_ERR_IO, "OHCI failed without reporting the reason");
  856. err = GRUB_USB_ERR_INTERNAL;
  857. break;
  858. case 1:
  859. /* XXX: CRC error. */
  860. err = GRUB_USB_ERR_TIMEOUT;
  861. break;
  862. case 2:
  863. err = GRUB_USB_ERR_BITSTUFF;
  864. break;
  865. case 3:
  866. /* XXX: Data Toggle error. */
  867. err = GRUB_USB_ERR_DATA;
  868. break;
  869. case 4:
  870. err = GRUB_USB_ERR_STALL;
  871. break;
  872. case 5:
  873. /* XXX: Not responding. */
  874. err = GRUB_USB_ERR_TIMEOUT;
  875. break;
  876. case 6:
  877. /* XXX: PID Check bits failed. */
  878. err = GRUB_USB_ERR_BABBLE;
  879. break;
  880. case 7:
  881. /* XXX: PID unexpected failed. */
  882. err = GRUB_USB_ERR_BABBLE;
  883. break;
  884. case 8:
  885. /* XXX: Data overrun error. */
  886. err = GRUB_USB_ERR_DATA;
  887. grub_dprintf ("ohci", "Overrun, failed TD address: %p, index: %d\n",
  888. tderr_virt, tderr_virt->tr_index);
  889. break;
  890. case 9:
  891. /* XXX: Data underrun error. */
  892. grub_dprintf ("ohci", "Underrun, failed TD address: %p, index: %d\n",
  893. tderr_virt, tderr_virt->tr_index);
  894. if (transfer->last_trans == -1)
  895. break;
  896. *actual = transfer->transactions[transfer->last_trans].size
  897. - (grub_le_to_cpu32 (tderr_virt->buffer_end)
  898. - grub_le_to_cpu32 (tderr_virt->buffer))
  899. + transfer->transactions[transfer->last_trans].preceding;
  900. err = GRUB_USB_ERR_NONE;
  901. break;
  902. case 10:
  903. /* XXX: Reserved. */
  904. err = GRUB_USB_ERR_NAK;
  905. break;
  906. case 11:
  907. /* XXX: Reserved. */
  908. err = GRUB_USB_ERR_NAK;
  909. break;
  910. case 12:
  911. /* XXX: Buffer overrun. */
  912. err = GRUB_USB_ERR_DATA;
  913. break;
  914. case 13:
  915. /* XXX: Buffer underrun. */
  916. err = GRUB_USB_ERR_DATA;
  917. break;
  918. default:
  919. err = GRUB_USB_ERR_NAK;
  920. break;
  921. }
  922. finish_transfer (dev, transfer);
  923. return err;
  924. }
  925. static grub_usb_err_t
  926. parse_success (grub_usb_controller_t dev,
  927. grub_usb_transfer_t transfer,
  928. grub_size_t *actual)
  929. {
  930. struct grub_ohci *o = dev->data;
  931. struct grub_ohci_transfer_controller_data *cdata = transfer->controller_data;
  932. grub_ohci_td_t tderr_virt = NULL;
  933. pre_finish_transfer (dev, transfer);
  934. /* I hope we can do it as transfer (most probably) finished OK */
  935. cdata->tderr_phys = cdata->td_last_phys;
  936. /* Prepare pointer to last processed TD */
  937. tderr_virt = grub_ohci_td_phys2virt (o, cdata->tderr_phys);
  938. /* Set index of last processed TD */
  939. if (tderr_virt)
  940. transfer->last_trans = tderr_virt->tr_index;
  941. else
  942. transfer->last_trans = -1;
  943. *actual = transfer->size + 1;
  944. finish_transfer (dev, transfer);
  945. return GRUB_USB_ERR_NONE;
  946. }
  947. static grub_usb_err_t
  948. parse_unrec (grub_usb_controller_t dev,
  949. grub_usb_transfer_t transfer,
  950. grub_size_t *actual)
  951. {
  952. struct grub_ohci *o = dev->data;
  953. *actual = 0;
  954. pre_finish_transfer (dev, transfer);
  955. /* Don't try to get error code and last processed TD for proper
  956. * toggle bit value - anything can be invalid */
  957. grub_dprintf("ohci", "Unrecoverable error!");
  958. /* Do OHCI reset in case of unrecoverable error - maybe we will need
  959. * do more - re-enumerate bus etc. (?) */
  960. /* Suspend the OHCI by issuing a reset. */
  961. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, 1); /* XXX: Magic. */
  962. /* Read back of register should ensure it is really written */
  963. grub_ohci_readreg32 (o, GRUB_OHCI_REG_CMDSTATUS);
  964. grub_millisleep (1);
  965. grub_dprintf ("ohci", "Unrecoverable error - OHCI reset\n");
  966. /* Misc. resets. */
  967. o->hcca->donehead = 0;
  968. grub_ohci_writereg32 (o, GRUB_OHCI_REG_INTSTATUS, 0x7f); /* Clears everything */
  969. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLHEAD, o->ed_ctrl_addr);
  970. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLCURR, 0);
  971. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKHEAD, o->ed_bulk_addr);
  972. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKCURR, 0);
  973. /* Read back of register should ensure it is really written */
  974. grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS);
  975. /* Enable the OHCI. */
  976. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL,
  977. (2 << 6)
  978. | GRUB_OHCI_REG_CONTROL_CONTROL_ENABLE
  979. | GRUB_OHCI_REG_CONTROL_BULK_ENABLE );
  980. finish_transfer (dev, transfer);
  981. return GRUB_USB_ERR_UNRECOVERABLE;
  982. }
  983. static grub_usb_err_t
  984. grub_ohci_check_transfer (grub_usb_controller_t dev,
  985. grub_usb_transfer_t transfer,
  986. grub_size_t *actual)
  987. {
  988. struct grub_ohci *o = dev->data;
  989. struct grub_ohci_transfer_controller_data *cdata = transfer->controller_data;
  990. grub_uint32_t intstatus;
  991. /* Check transfer status */
  992. intstatus = grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS);
  993. if ((intstatus & 0x10) != 0)
  994. /* Unrecoverable error - only reset can help...! */
  995. return parse_unrec (dev, transfer, actual);
  996. /* Detected a HALT. */
  997. if ((grub_le_to_cpu32 (cdata->ed_virt->td_head) & 1))
  998. return parse_halt (dev, transfer, actual);
  999. /* Finished ED detection */
  1000. if ( (grub_le_to_cpu32 (cdata->ed_virt->td_head) & ~0xfU) ==
  1001. (grub_le_to_cpu32 (cdata->ed_virt->td_tail) & ~0xfU) ) /* Empty ED */
  1002. {
  1003. /* Check the HALT bit */
  1004. /* It looks like nonsense - it was tested previously...
  1005. * but it can change because OHCI is working
  1006. * simultaneously via DMA... */
  1007. if (grub_le_to_cpu32 (cdata->ed_virt->td_head) & 1)
  1008. return parse_halt (dev, transfer, actual);
  1009. else
  1010. return parse_success (dev, transfer, actual);
  1011. }
  1012. return GRUB_USB_ERR_WAIT;
  1013. }
  1014. static grub_usb_err_t
  1015. grub_ohci_cancel_transfer (grub_usb_controller_t dev,
  1016. grub_usb_transfer_t transfer)
  1017. {
  1018. struct grub_ohci *o = dev->data;
  1019. struct grub_ohci_transfer_controller_data *cdata = transfer->controller_data;
  1020. grub_ohci_td_t tderr_virt = NULL;
  1021. pre_finish_transfer (dev, transfer);
  1022. grub_dprintf("ohci", "Timeout !\n");
  1023. /* We should wait for next SOF to be sure that ED is unaccessed
  1024. * by OHCI */
  1025. /* SF bit reset. (SF bit indicates Start Of Frame (SOF) packet) */
  1026. grub_ohci_writereg32 (o, GRUB_OHCI_REG_INTSTATUS, (1<<2));
  1027. /* Wait for new SOF */
  1028. while ((grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS) & 0x4) == 0);
  1029. /* Possible retired TD with error should be previous TD to ED->td_head */
  1030. cdata->tderr_phys
  1031. = grub_ohci_td_phys2virt (o, grub_le_to_cpu32 (cdata->ed_virt->td_head)
  1032. & ~0xf)->prev_td_phys;
  1033. tderr_virt = grub_ohci_td_phys2virt (o,cdata-> tderr_phys);
  1034. grub_dprintf ("ohci", "Cancel: tderr_phys=0x%x, tderr_virt=%p\n",
  1035. cdata->tderr_phys, tderr_virt);
  1036. if (tderr_virt)
  1037. transfer->last_trans = tderr_virt->tr_index;
  1038. else
  1039. transfer->last_trans = -1;
  1040. finish_transfer (dev, transfer);
  1041. return GRUB_USB_ERR_NONE;
  1042. }
  1043. static grub_usb_err_t
  1044. grub_ohci_portstatus (grub_usb_controller_t dev,
  1045. unsigned int port, unsigned int enable)
  1046. {
  1047. struct grub_ohci *o = (struct grub_ohci *) dev->data;
  1048. grub_uint64_t endtime;
  1049. int i;
  1050. grub_dprintf ("ohci", "begin of portstatus=0x%02x\n",
  1051. grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port));
  1052. if (!enable) /* We don't need reset port */
  1053. {
  1054. /* Disable the port and wait for it. */
  1055. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + port,
  1056. GRUB_OHCI_CLEAR_PORT_ENABLE);
  1057. endtime = grub_get_time_ms () + 1000;
  1058. while ((grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port)
  1059. & (1 << 1)))
  1060. if (grub_get_time_ms () > endtime)
  1061. return GRUB_USB_ERR_TIMEOUT;
  1062. grub_dprintf ("ohci", "end of portstatus=0x%02x\n",
  1063. grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port));
  1064. return GRUB_USB_ERR_NONE;
  1065. }
  1066. /* OHCI does one reset signal 10ms long but USB spec.
  1067. * requests 50ms for root hub (no need to be continuous).
  1068. * So, we do reset 5 times... */
  1069. for (i = 0; i < 5; i++)
  1070. {
  1071. /* Reset the port - timing of reset is done by OHCI */
  1072. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + port,
  1073. GRUB_OHCI_SET_PORT_RESET);
  1074. /* Wait for reset completion */
  1075. endtime = grub_get_time_ms () + 1000;
  1076. while (! (grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port)
  1077. & GRUB_OHCI_SET_PORT_RESET_STATUS_CHANGE))
  1078. if (grub_get_time_ms () > endtime)
  1079. return GRUB_USB_ERR_TIMEOUT;
  1080. /* End the reset signaling - reset the reset status change */
  1081. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + port,
  1082. GRUB_OHCI_SET_PORT_RESET_STATUS_CHANGE);
  1083. grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port);
  1084. }
  1085. /* Enable port */
  1086. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + port,
  1087. GRUB_OHCI_SET_PORT_ENABLE);
  1088. grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port);
  1089. /* Wait for signal enabled */
  1090. endtime = grub_get_time_ms () + 1000;
  1091. while (! (grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port)
  1092. & (1 << 1)))
  1093. if (grub_get_time_ms () > endtime)
  1094. return GRUB_USB_ERR_TIMEOUT;
  1095. /* Reset bit Connect Status Change */
  1096. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + port,
  1097. GRUB_OHCI_RESET_CONNECT_CHANGE);
  1098. /* "Reset recovery time" (USB spec.) */
  1099. grub_millisleep (10);
  1100. grub_dprintf ("ohci", "end of portstatus=0x%02x\n",
  1101. grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port));
  1102. return GRUB_USB_ERR_NONE;
  1103. }
  1104. static grub_usb_speed_t
  1105. grub_ohci_detect_dev (grub_usb_controller_t dev, int port, int *changed)
  1106. {
  1107. struct grub_ohci *o = (struct grub_ohci *) dev->data;
  1108. grub_uint32_t status;
  1109. status = grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBPORT + port);
  1110. grub_dprintf ("ohci", "detect_dev status=0x%02x\n", status);
  1111. /* Connect Status Change bit - it detects change of connection */
  1112. if (status & GRUB_OHCI_RESET_CONNECT_CHANGE)
  1113. {
  1114. *changed = 1;
  1115. /* Reset bit Connect Status Change */
  1116. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + port,
  1117. GRUB_OHCI_RESET_CONNECT_CHANGE);
  1118. }
  1119. else
  1120. *changed = 0;
  1121. if (! (status & 1))
  1122. return GRUB_USB_SPEED_NONE;
  1123. else if (status & (1 << 9))
  1124. return GRUB_USB_SPEED_LOW;
  1125. else
  1126. return GRUB_USB_SPEED_FULL;
  1127. }
  1128. static int
  1129. grub_ohci_hubports (grub_usb_controller_t dev)
  1130. {
  1131. struct grub_ohci *o = (struct grub_ohci *) dev->data;
  1132. grub_uint32_t portinfo;
  1133. portinfo = grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBA);
  1134. grub_dprintf ("ohci", "root hub ports=%d\n", portinfo & 0xFF);
  1135. return portinfo & 0xFF;
  1136. }
  1137. static grub_err_t
  1138. grub_ohci_fini_hw (int noreturn __attribute__ ((unused)))
  1139. {
  1140. struct grub_ohci *o;
  1141. for (o = ohci; o; o = o->next)
  1142. {
  1143. int i, nports = grub_ohci_readreg32 (o, GRUB_OHCI_REG_RHUBA) & 0xff;
  1144. grub_uint64_t maxtime;
  1145. /* Set skip in all EDs */
  1146. if (o->ed_bulk)
  1147. for (i=0; i < GRUB_OHCI_BULK_EDS; i++)
  1148. o->ed_bulk[i].target |= grub_cpu_to_le32_compile_time (1 << 14); /* skip */
  1149. if (o->ed_ctrl)
  1150. for (i=0; i < GRUB_OHCI_CTRL_EDS; i++)
  1151. o->ed_ctrl[i].target |= grub_cpu_to_le32_compile_time (1 << 14); /* skip */
  1152. /* We should wait for next SOF to be sure that all EDs are
  1153. * unaccessed by OHCI. But OHCI can be non-functional, so
  1154. * more than 1ms timeout have to be applied. */
  1155. /* SF bit reset. (SF bit indicates Start Of Frame (SOF) packet) */
  1156. grub_ohci_writereg32 (o, GRUB_OHCI_REG_INTSTATUS, (1<<2));
  1157. maxtime = grub_get_time_ms () + 2;
  1158. /* Wait for new SOF or timeout */
  1159. while ( ((grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS) & 0x4)
  1160. == 0) || (grub_get_time_ms () >= maxtime) );
  1161. for (i = 0; i < nports; i++)
  1162. grub_ohci_writereg32 (o, GRUB_OHCI_REG_RHUBPORT + i,
  1163. GRUB_OHCI_CLEAR_PORT_ENABLE);
  1164. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, 1);
  1165. grub_millisleep (1);
  1166. grub_ohci_writereg32 (o, GRUB_OHCI_REG_HCCA, 0);
  1167. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLHEAD, 0);
  1168. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLCURR, 0);
  1169. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKHEAD, 0);
  1170. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKCURR, 0);
  1171. grub_ohci_writereg32 (o, GRUB_OHCI_REG_DONEHEAD, 0);
  1172. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL, 0);
  1173. /* Read back of register should ensure it is really written */
  1174. grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS);
  1175. #if 0 /* Is this necessary before booting? Probably not .(?)
  1176. * But it must be done if module is removed ! (Or not ?)
  1177. * How to do it ? - Probably grub_ohci_restore_hw should be more
  1178. * complicated. (?)
  1179. * (If we do it, we need to reallocate EDs and TDs in function
  1180. * grub_ohci_restore_hw ! */
  1181. /* Free allocated EDs and TDs */
  1182. grub_dma_free (o->td_chunk);
  1183. grub_dma_free (o->ed_bulk_chunk);
  1184. grub_dma_free (o->ed_ctrl_chunk);
  1185. grub_dma_free (o->hcca_chunk);
  1186. #endif
  1187. }
  1188. grub_millisleep (10);
  1189. return GRUB_ERR_NONE;
  1190. }
  1191. static grub_err_t
  1192. grub_ohci_restore_hw (void)
  1193. {
  1194. struct grub_ohci *o;
  1195. for (o = ohci; o; o = o->next)
  1196. {
  1197. grub_ohci_writereg32 (o, GRUB_OHCI_REG_HCCA, o->hcca_addr);
  1198. o->hcca->donehead = 0;
  1199. grub_ohci_writereg32 (o, GRUB_OHCI_REG_INTSTATUS, 0x7f); /* Clears everything */
  1200. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLHEAD, o->ed_ctrl_addr);
  1201. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLCURR, 0);
  1202. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKHEAD, o->ed_bulk_addr);
  1203. grub_ohci_writereg32 (o, GRUB_OHCI_REG_BULKCURR, 0);
  1204. /* Read back of register should ensure it is really written */
  1205. grub_ohci_readreg32 (o, GRUB_OHCI_REG_INTSTATUS);
  1206. /* Enable the OHCI. */
  1207. grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL,
  1208. (2 << 6)
  1209. | GRUB_OHCI_REG_CONTROL_CONTROL_ENABLE
  1210. | GRUB_OHCI_REG_CONTROL_BULK_ENABLE );
  1211. }
  1212. return GRUB_ERR_NONE;
  1213. }
  1214. static struct grub_usb_controller_dev usb_controller =
  1215. {
  1216. .name = "ohci",
  1217. .iterate = grub_ohci_iterate,
  1218. .setup_transfer = grub_ohci_setup_transfer,
  1219. .check_transfer = grub_ohci_check_transfer,
  1220. .cancel_transfer = grub_ohci_cancel_transfer,
  1221. .hubports = grub_ohci_hubports,
  1222. .portstatus = grub_ohci_portstatus,
  1223. .detect_dev = grub_ohci_detect_dev,
  1224. /* estimated max. count of TDs for one bulk transfer */
  1225. .max_bulk_tds = GRUB_OHCI_TDS * 3 / 4
  1226. };
  1227. static struct grub_preboot *fini_hnd;
  1228. GRUB_MOD_INIT(ohci)
  1229. {
  1230. COMPILE_TIME_ASSERT (sizeof (struct grub_ohci_td) == 32);
  1231. COMPILE_TIME_ASSERT (sizeof (struct grub_ohci_ed) == 16);
  1232. grub_stop_disk_firmware ();
  1233. grub_ohci_inithw ();
  1234. grub_usb_controller_dev_register (&usb_controller);
  1235. fini_hnd = grub_loader_register_preboot_hook (grub_ohci_fini_hw,
  1236. grub_ohci_restore_hw,
  1237. GRUB_LOADER_PREBOOT_HOOK_PRIO_DISK);
  1238. }
  1239. GRUB_MOD_FINI(ohci)
  1240. {
  1241. grub_ohci_fini_hw (0);
  1242. grub_loader_unregister_preboot_hook (fini_hnd);
  1243. grub_usb_controller_dev_unregister (&usb_controller);
  1244. }