fw-ohci.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-ohci.c - Driver for OHCI 1394 boards
  4. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program 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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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 this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/poll.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/semaphore.h>
  29. #include "fw-transaction.h"
  30. #include "fw-ohci.h"
  31. #define descriptor_output_more 0
  32. #define descriptor_output_last (1 << 12)
  33. #define descriptor_input_more (2 << 12)
  34. #define descriptor_input_last (3 << 12)
  35. #define descriptor_status (1 << 11)
  36. #define descriptor_key_immediate (2 << 8)
  37. #define descriptor_ping (1 << 7)
  38. #define descriptor_yy (1 << 6)
  39. #define descriptor_no_irq (0 << 4)
  40. #define descriptor_irq_error (1 << 4)
  41. #define descriptor_irq_always (3 << 4)
  42. #define descriptor_branch_always (3 << 2)
  43. struct descriptor {
  44. __le16 req_count;
  45. __le16 control;
  46. __le32 data_address;
  47. __le32 branch_address;
  48. __le16 res_count;
  49. __le16 transfer_status;
  50. } __attribute__((aligned(16)));
  51. struct ar_context {
  52. struct fw_ohci *ohci;
  53. struct descriptor descriptor;
  54. __le32 buffer[512];
  55. dma_addr_t descriptor_bus;
  56. dma_addr_t buffer_bus;
  57. u32 command_ptr;
  58. u32 control_set;
  59. u32 control_clear;
  60. struct tasklet_struct tasklet;
  61. };
  62. struct at_context {
  63. struct fw_ohci *ohci;
  64. dma_addr_t descriptor_bus;
  65. dma_addr_t buffer_bus;
  66. struct list_head list;
  67. struct {
  68. struct descriptor more;
  69. __le32 header[4];
  70. struct descriptor last;
  71. } d;
  72. u32 command_ptr;
  73. u32 control_set;
  74. u32 control_clear;
  75. struct tasklet_struct tasklet;
  76. };
  77. #define it_header_sy(v) ((v) << 0)
  78. #define it_header_tcode(v) ((v) << 4)
  79. #define it_header_channel(v) ((v) << 8)
  80. #define it_header_tag(v) ((v) << 14)
  81. #define it_header_speed(v) ((v) << 16)
  82. #define it_header_data_length(v) ((v) << 16)
  83. struct iso_context {
  84. struct fw_iso_context base;
  85. struct tasklet_struct tasklet;
  86. u32 control_set;
  87. u32 control_clear;
  88. u32 command_ptr;
  89. u32 context_match;
  90. struct descriptor *buffer;
  91. dma_addr_t buffer_bus;
  92. struct descriptor *head_descriptor;
  93. struct descriptor *tail_descriptor;
  94. struct descriptor *tail_descriptor_last;
  95. struct descriptor *prev_descriptor;
  96. };
  97. #define CONFIG_ROM_SIZE 1024
  98. struct fw_ohci {
  99. struct fw_card card;
  100. __iomem char *registers;
  101. dma_addr_t self_id_bus;
  102. __le32 *self_id_cpu;
  103. struct tasklet_struct bus_reset_tasklet;
  104. int generation;
  105. int request_generation;
  106. /* Spinlock for accessing fw_ohci data. Never call out of
  107. * this driver with this lock held. */
  108. spinlock_t lock;
  109. u32 self_id_buffer[512];
  110. /* Config rom buffers */
  111. __be32 *config_rom;
  112. dma_addr_t config_rom_bus;
  113. __be32 *next_config_rom;
  114. dma_addr_t next_config_rom_bus;
  115. u32 next_header;
  116. struct ar_context ar_request_ctx;
  117. struct ar_context ar_response_ctx;
  118. struct at_context at_request_ctx;
  119. struct at_context at_response_ctx;
  120. u32 it_context_mask;
  121. struct iso_context *it_context_list;
  122. u32 ir_context_mask;
  123. struct iso_context *ir_context_list;
  124. };
  125. extern inline struct fw_ohci *fw_ohci(struct fw_card *card)
  126. {
  127. return container_of(card, struct fw_ohci, card);
  128. }
  129. #define CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  130. #define CONTEXT_RUN 0x8000
  131. #define CONTEXT_WAKE 0x1000
  132. #define CONTEXT_DEAD 0x0800
  133. #define CONTEXT_ACTIVE 0x0400
  134. #define OHCI1394_MAX_AT_REQ_RETRIES 0x2
  135. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  136. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  137. #define FW_OHCI_MAJOR 240
  138. #define OHCI1394_REGISTER_SIZE 0x800
  139. #define OHCI_LOOP_COUNT 500
  140. #define OHCI1394_PCI_HCI_Control 0x40
  141. #define SELF_ID_BUF_SIZE 0x800
  142. /* FIXME: Move this to linux/pci_ids.h */
  143. #define PCI_CLASS_SERIAL_FIREWIRE_OHCI 0x0c0010
  144. static char ohci_driver_name[] = KBUILD_MODNAME;
  145. extern inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  146. {
  147. writel(data, ohci->registers + offset);
  148. }
  149. extern inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  150. {
  151. return readl(ohci->registers + offset);
  152. }
  153. extern inline void flush_writes(const struct fw_ohci *ohci)
  154. {
  155. /* Do a dummy read to flush writes. */
  156. reg_read(ohci, OHCI1394_Version);
  157. }
  158. static int
  159. ohci_update_phy_reg(struct fw_card *card, int addr,
  160. int clear_bits, int set_bits)
  161. {
  162. struct fw_ohci *ohci = fw_ohci(card);
  163. u32 val, old;
  164. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  165. msleep(2);
  166. val = reg_read(ohci, OHCI1394_PhyControl);
  167. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  168. fw_error("failed to set phy reg bits.\n");
  169. return -EBUSY;
  170. }
  171. old = OHCI1394_PhyControl_ReadData(val);
  172. old = (old & ~clear_bits) | set_bits;
  173. reg_write(ohci, OHCI1394_PhyControl,
  174. OHCI1394_PhyControl_Write(addr, old));
  175. return 0;
  176. }
  177. static void ar_context_run(struct ar_context *ctx)
  178. {
  179. reg_write(ctx->ohci, ctx->command_ptr, ctx->descriptor_bus | 1);
  180. reg_write(ctx->ohci, ctx->control_set, CONTEXT_RUN);
  181. flush_writes(ctx->ohci);
  182. }
  183. static void ar_context_tasklet(unsigned long data)
  184. {
  185. struct ar_context *ctx = (struct ar_context *)data;
  186. struct fw_ohci *ohci = ctx->ohci;
  187. u32 status;
  188. int length, speed, ack, timestamp, tcode;
  189. /* FIXME: What to do about evt_* errors? */
  190. length = le16_to_cpu(ctx->descriptor.req_count) -
  191. le16_to_cpu(ctx->descriptor.res_count) - 4;
  192. status = le32_to_cpu(ctx->buffer[length / 4]);
  193. ack = ((status >> 16) & 0x1f) - 16;
  194. speed = (status >> 21) & 0x7;
  195. timestamp = status & 0xffff;
  196. ctx->buffer[0] = le32_to_cpu(ctx->buffer[0]);
  197. ctx->buffer[1] = le32_to_cpu(ctx->buffer[1]);
  198. ctx->buffer[2] = le32_to_cpu(ctx->buffer[2]);
  199. tcode = (ctx->buffer[0] >> 4) & 0x0f;
  200. if (TCODE_IS_BLOCK_PACKET(tcode))
  201. ctx->buffer[3] = le32_to_cpu(ctx->buffer[3]);
  202. /* The OHCI bus reset handler synthesizes a phy packet with
  203. * the new generation number when a bus reset happens (see
  204. * section 8.4.2.3). This helps us determine when a request
  205. * was received and make sure we send the response in the same
  206. * generation. We only need this for requests; for responses
  207. * we use the unique tlabel for finding the matching
  208. * request. */
  209. if (ack + 16 == 0x09)
  210. ohci->request_generation = (ctx->buffer[2] >> 16) & 0xff;
  211. else if (ctx == &ohci->ar_request_ctx)
  212. fw_core_handle_request(&ohci->card, speed, ack, timestamp,
  213. ohci->request_generation,
  214. length, ctx->buffer);
  215. else
  216. fw_core_handle_response(&ohci->card, speed, ack, timestamp,
  217. length, ctx->buffer);
  218. ctx->descriptor.data_address = cpu_to_le32(ctx->buffer_bus);
  219. ctx->descriptor.req_count = cpu_to_le16(sizeof ctx->buffer);
  220. ctx->descriptor.res_count = cpu_to_le16(sizeof ctx->buffer);
  221. dma_sync_single_for_device(ohci->card.device, ctx->descriptor_bus,
  222. sizeof ctx->descriptor_bus, DMA_TO_DEVICE);
  223. /* FIXME: We stop and restart the ar context here, what if we
  224. * stop while a receive is in progress? Maybe we could just
  225. * loop the context back to itself and use it in buffer fill
  226. * mode as intended... */
  227. reg_write(ctx->ohci, ctx->control_clear, CONTEXT_RUN);
  228. ar_context_run(ctx);
  229. }
  230. static int
  231. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 control_set)
  232. {
  233. ctx->descriptor_bus =
  234. dma_map_single(ohci->card.device, &ctx->descriptor,
  235. sizeof ctx->descriptor, DMA_TO_DEVICE);
  236. if (ctx->descriptor_bus == 0)
  237. return -ENOMEM;
  238. if (ctx->descriptor_bus & 0xf)
  239. fw_notify("descriptor not 16-byte aligned: 0x%08x\n",
  240. ctx->descriptor_bus);
  241. ctx->buffer_bus =
  242. dma_map_single(ohci->card.device, ctx->buffer,
  243. sizeof ctx->buffer, DMA_FROM_DEVICE);
  244. if (ctx->buffer_bus == 0) {
  245. dma_unmap_single(ohci->card.device, ctx->descriptor_bus,
  246. sizeof ctx->descriptor, DMA_TO_DEVICE);
  247. return -ENOMEM;
  248. }
  249. memset(&ctx->descriptor, 0, sizeof ctx->descriptor);
  250. ctx->descriptor.control = cpu_to_le16(descriptor_input_more |
  251. descriptor_status |
  252. descriptor_branch_always);
  253. ctx->descriptor.req_count = cpu_to_le16(sizeof ctx->buffer);
  254. ctx->descriptor.data_address = cpu_to_le32(ctx->buffer_bus);
  255. ctx->descriptor.res_count = cpu_to_le16(sizeof ctx->buffer);
  256. ctx->control_set = control_set;
  257. ctx->control_clear = control_set + 4;
  258. ctx->command_ptr = control_set + 12;
  259. ctx->ohci = ohci;
  260. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  261. ar_context_run(ctx);
  262. return 0;
  263. }
  264. static void
  265. do_packet_callbacks(struct fw_ohci *ohci, struct list_head *list)
  266. {
  267. struct fw_packet *p, *next;
  268. list_for_each_entry_safe(p, next, list, link)
  269. p->callback(p, &ohci->card, p->status);
  270. }
  271. static void
  272. complete_transmission(struct fw_packet *packet,
  273. int status, struct list_head *list)
  274. {
  275. list_move_tail(&packet->link, list);
  276. packet->status = status;
  277. }
  278. /* This function prepares the first packet in the context queue for
  279. * transmission. Must always be called with the ochi->lock held to
  280. * ensure proper generation handling and locking around packet queue
  281. * manipulation. */
  282. static void
  283. at_context_setup_packet(struct at_context *ctx, struct list_head *list)
  284. {
  285. struct fw_packet *packet;
  286. struct fw_ohci *ohci = ctx->ohci;
  287. int z, tcode;
  288. packet = fw_packet(ctx->list.next);
  289. memset(&ctx->d, 0, sizeof ctx->d);
  290. if (packet->payload_length > 0) {
  291. packet->payload_bus = dma_map_single(ohci->card.device,
  292. packet->payload,
  293. packet->payload_length,
  294. DMA_TO_DEVICE);
  295. if (packet->payload_bus == 0) {
  296. complete_transmission(packet, -ENOMEM, list);
  297. return;
  298. }
  299. ctx->d.more.control =
  300. cpu_to_le16(descriptor_output_more |
  301. descriptor_key_immediate);
  302. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  303. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  304. ctx->d.last.control =
  305. cpu_to_le16(descriptor_output_last |
  306. descriptor_irq_always |
  307. descriptor_branch_always);
  308. ctx->d.last.req_count = cpu_to_le16(packet->payload_length);
  309. ctx->d.last.data_address = cpu_to_le32(packet->payload_bus);
  310. z = 3;
  311. } else {
  312. ctx->d.more.control =
  313. cpu_to_le16(descriptor_output_last |
  314. descriptor_key_immediate |
  315. descriptor_irq_always |
  316. descriptor_branch_always);
  317. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  318. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  319. z = 2;
  320. }
  321. /* The DMA format for asyncronous link packets is different
  322. * from the IEEE1394 layout, so shift the fields around
  323. * accordingly. If header_length is 8, it's a PHY packet, to
  324. * which we need to prepend an extra quadlet. */
  325. if (packet->header_length > 8) {
  326. ctx->d.header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  327. (packet->speed << 16));
  328. ctx->d.header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  329. (packet->header[0] & 0xffff0000));
  330. ctx->d.header[2] = cpu_to_le32(packet->header[2]);
  331. tcode = (packet->header[0] >> 4) & 0x0f;
  332. if (TCODE_IS_BLOCK_PACKET(tcode))
  333. ctx->d.header[3] = cpu_to_le32(packet->header[3]);
  334. else
  335. ctx->d.header[3] = packet->header[3];
  336. } else {
  337. ctx->d.header[0] =
  338. cpu_to_le32((OHCI1394_phy_tcode << 4) |
  339. (packet->speed << 16));
  340. ctx->d.header[1] = cpu_to_le32(packet->header[0]);
  341. ctx->d.header[2] = cpu_to_le32(packet->header[1]);
  342. ctx->d.more.req_count = cpu_to_le16(12);
  343. }
  344. /* FIXME: Document how the locking works. */
  345. if (ohci->generation == packet->generation) {
  346. reg_write(ctx->ohci, ctx->command_ptr,
  347. ctx->descriptor_bus | z);
  348. reg_write(ctx->ohci, ctx->control_set,
  349. CONTEXT_RUN | CONTEXT_WAKE);
  350. } else {
  351. /* We dont return error codes from this function; all
  352. * transmission errors are reported through the
  353. * callback. */
  354. complete_transmission(packet, -ESTALE, list);
  355. }
  356. }
  357. static void at_context_stop(struct at_context *ctx)
  358. {
  359. u32 reg;
  360. reg_write(ctx->ohci, ctx->control_clear, CONTEXT_RUN);
  361. reg = reg_read(ctx->ohci, ctx->control_set);
  362. if (reg & CONTEXT_ACTIVE)
  363. fw_notify("Tried to stop context, but it is still active "
  364. "(0x%08x).\n", reg);
  365. }
  366. static void at_context_tasklet(unsigned long data)
  367. {
  368. struct at_context *ctx = (struct at_context *)data;
  369. struct fw_ohci *ohci = ctx->ohci;
  370. struct fw_packet *packet;
  371. LIST_HEAD(list);
  372. unsigned long flags;
  373. int evt;
  374. spin_lock_irqsave(&ohci->lock, flags);
  375. packet = fw_packet(ctx->list.next);
  376. at_context_stop(ctx);
  377. if (packet->payload_length > 0) {
  378. dma_unmap_single(ohci->card.device, packet->payload_bus,
  379. packet->payload_length, DMA_TO_DEVICE);
  380. evt = le16_to_cpu(ctx->d.last.transfer_status) & 0x1f;
  381. packet->timestamp = le16_to_cpu(ctx->d.last.res_count);
  382. }
  383. else {
  384. evt = le16_to_cpu(ctx->d.more.transfer_status) & 0x1f;
  385. packet->timestamp = le16_to_cpu(ctx->d.more.res_count);
  386. }
  387. if (evt < 16) {
  388. switch (evt) {
  389. case OHCI1394_evt_timeout:
  390. /* Async response transmit timed out. */
  391. complete_transmission(packet, -ETIMEDOUT, &list);
  392. break;
  393. case OHCI1394_evt_flushed:
  394. /* The packet was flushed should give same
  395. * error as when we try to use a stale
  396. * generation count. */
  397. complete_transmission(packet, -ESTALE, &list);
  398. break;
  399. case OHCI1394_evt_missing_ack:
  400. /* This would be a higher level software
  401. * error, it is using a valid (current)
  402. * generation count, but the node is not on
  403. * the bus. */
  404. complete_transmission(packet, -ENODEV, &list);
  405. break;
  406. default:
  407. complete_transmission(packet, -EIO, &list);
  408. break;
  409. }
  410. } else
  411. complete_transmission(packet, evt - 16, &list);
  412. /* If more packets are queued, set up the next one. */
  413. if (!list_empty(&ctx->list))
  414. at_context_setup_packet(ctx, &list);
  415. spin_unlock_irqrestore(&ohci->lock, flags);
  416. do_packet_callbacks(ohci, &list);
  417. }
  418. static int
  419. at_context_init(struct at_context *ctx, struct fw_ohci *ohci, u32 control_set)
  420. {
  421. INIT_LIST_HEAD(&ctx->list);
  422. ctx->descriptor_bus =
  423. dma_map_single(ohci->card.device, &ctx->d,
  424. sizeof ctx->d, DMA_TO_DEVICE);
  425. if (ctx->descriptor_bus == 0)
  426. return -ENOMEM;
  427. ctx->control_set = control_set;
  428. ctx->control_clear = control_set + 4;
  429. ctx->command_ptr = control_set + 12;
  430. ctx->ohci = ohci;
  431. tasklet_init(&ctx->tasklet, at_context_tasklet, (unsigned long)ctx);
  432. return 0;
  433. }
  434. static void
  435. at_context_transmit(struct at_context *ctx, struct fw_packet *packet)
  436. {
  437. LIST_HEAD(list);
  438. unsigned long flags;
  439. int was_empty;
  440. spin_lock_irqsave(&ctx->ohci->lock, flags);
  441. was_empty = list_empty(&ctx->list);
  442. list_add_tail(&packet->link, &ctx->list);
  443. if (was_empty)
  444. at_context_setup_packet(ctx, &list);
  445. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  446. do_packet_callbacks(ctx->ohci, &list);
  447. }
  448. static void bus_reset_tasklet(unsigned long data)
  449. {
  450. struct fw_ohci *ohci = (struct fw_ohci *)data;
  451. int self_id_count, i, j, reg, node_id;
  452. int generation, new_generation;
  453. unsigned long flags;
  454. reg = reg_read(ohci, OHCI1394_NodeID);
  455. if (!(reg & OHCI1394_NodeID_idValid)) {
  456. fw_error("node ID not valid, new bus reset in progress\n");
  457. return;
  458. }
  459. node_id = reg & 0xffff;
  460. /* The count in the SelfIDCount register is the number of
  461. * bytes in the self ID receive buffer. Since we also receive
  462. * the inverted quadlets and a header quadlet, we shift one
  463. * bit extra to get the actual number of self IDs. */
  464. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  465. generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  466. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  467. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  468. fw_error("inconsistent self IDs\n");
  469. ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
  470. }
  471. /* Check the consistency of the self IDs we just read. The
  472. * problem we face is that a new bus reset can start while we
  473. * read out the self IDs from the DMA buffer. If this happens,
  474. * the DMA buffer will be overwritten with new self IDs and we
  475. * will read out inconsistent data. The OHCI specification
  476. * (section 11.2) recommends a technique similar to
  477. * linux/seqlock.h, where we remember the generation of the
  478. * self IDs in the buffer before reading them out and compare
  479. * it to the current generation after reading them out. If
  480. * the two generations match we know we have a consistent set
  481. * of self IDs. */
  482. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  483. if (new_generation != generation) {
  484. fw_notify("recursive bus reset detected, "
  485. "discarding self ids\n");
  486. return;
  487. }
  488. /* FIXME: Document how the locking works. */
  489. spin_lock_irqsave(&ohci->lock, flags);
  490. ohci->generation = generation;
  491. at_context_stop(&ohci->at_request_ctx);
  492. at_context_stop(&ohci->at_response_ctx);
  493. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  494. /* This next bit is unrelated to the AT context stuff but we
  495. * have to do it under the spinlock also. If a new config rom
  496. * was set up before this reset, the old one is now no longer
  497. * in use and we can free it. Update the config rom pointers
  498. * to point to the current config rom and clear the
  499. * next_config_rom pointer so a new udpate can take place. */
  500. if (ohci->next_config_rom != NULL) {
  501. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  502. ohci->config_rom, ohci->config_rom_bus);
  503. ohci->config_rom = ohci->next_config_rom;
  504. ohci->config_rom_bus = ohci->next_config_rom_bus;
  505. ohci->next_config_rom = NULL;
  506. /* Restore config_rom image and manually update
  507. * config_rom registers. Writing the header quadlet
  508. * will indicate that the config rom is ready, so we
  509. * do that last. */
  510. reg_write(ohci, OHCI1394_BusOptions,
  511. be32_to_cpu(ohci->config_rom[2]));
  512. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  513. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  514. }
  515. spin_unlock_irqrestore(&ohci->lock, flags);
  516. fw_core_handle_bus_reset(&ohci->card, node_id, generation,
  517. self_id_count, ohci->self_id_buffer);
  518. }
  519. static irqreturn_t irq_handler(int irq, void *data)
  520. {
  521. struct fw_ohci *ohci = data;
  522. u32 event, iso_event;
  523. int i;
  524. event = reg_read(ohci, OHCI1394_IntEventClear);
  525. if (!event)
  526. return IRQ_NONE;
  527. reg_write(ohci, OHCI1394_IntEventClear, event);
  528. if (event & OHCI1394_selfIDComplete)
  529. tasklet_schedule(&ohci->bus_reset_tasklet);
  530. if (event & OHCI1394_RQPkt)
  531. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  532. if (event & OHCI1394_RSPkt)
  533. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  534. if (event & OHCI1394_reqTxComplete)
  535. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  536. if (event & OHCI1394_respTxComplete)
  537. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  538. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventSet);
  539. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  540. while (iso_event) {
  541. i = ffs(iso_event) - 1;
  542. tasklet_schedule(&ohci->ir_context_list[i].tasklet);
  543. iso_event &= ~(1 << i);
  544. }
  545. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventSet);
  546. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  547. while (iso_event) {
  548. i = ffs(iso_event) - 1;
  549. tasklet_schedule(&ohci->it_context_list[i].tasklet);
  550. iso_event &= ~(1 << i);
  551. }
  552. return IRQ_HANDLED;
  553. }
  554. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  555. {
  556. struct fw_ohci *ohci = fw_ohci(card);
  557. struct pci_dev *dev = to_pci_dev(card->device);
  558. /* When the link is not yet enabled, the atomic config rom
  559. * update mechanism described below in ohci_set_config_rom()
  560. * is not active. We have to update ConfigRomHeader and
  561. * BusOptions manually, and the write to ConfigROMmap takes
  562. * effect immediately. We tie this to the enabling of the
  563. * link, so we have a valid config rom before enabling - the
  564. * OHCI requires that ConfigROMhdr and BusOptions have valid
  565. * values before enabling.
  566. *
  567. * However, when the ConfigROMmap is written, some controllers
  568. * always read back quadlets 0 and 2 from the config rom to
  569. * the ConfigRomHeader and BusOptions registers on bus reset.
  570. * They shouldn't do that in this initial case where the link
  571. * isn't enabled. This means we have to use the same
  572. * workaround here, setting the bus header to 0 and then write
  573. * the right values in the bus reset tasklet.
  574. */
  575. ohci->next_config_rom =
  576. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  577. &ohci->next_config_rom_bus, GFP_KERNEL);
  578. if (ohci->next_config_rom == NULL)
  579. return -ENOMEM;
  580. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  581. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  582. ohci->next_header = config_rom[0];
  583. ohci->next_config_rom[0] = 0;
  584. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  585. reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
  586. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  587. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  588. if (request_irq(dev->irq, irq_handler,
  589. SA_SHIRQ, ohci_driver_name, ohci)) {
  590. fw_error("Failed to allocate shared interrupt %d.\n",
  591. dev->irq);
  592. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  593. ohci->config_rom, ohci->config_rom_bus);
  594. return -EIO;
  595. }
  596. reg_write(ohci, OHCI1394_HCControlSet,
  597. OHCI1394_HCControl_linkEnable |
  598. OHCI1394_HCControl_BIBimageValid);
  599. flush_writes(ohci);
  600. /* We are ready to go, initiate bus reset to finish the
  601. * initialization. */
  602. fw_core_initiate_bus_reset(&ohci->card, 1);
  603. return 0;
  604. }
  605. static int
  606. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  607. {
  608. struct fw_ohci *ohci;
  609. unsigned long flags;
  610. int retval = 0;
  611. __be32 *next_config_rom;
  612. dma_addr_t next_config_rom_bus;
  613. ohci = fw_ohci(card);
  614. /* When the OHCI controller is enabled, the config rom update
  615. * mechanism is a bit tricky, but easy enough to use. See
  616. * section 5.5.6 in the OHCI specification.
  617. *
  618. * The OHCI controller caches the new config rom address in a
  619. * shadow register (ConfigROMmapNext) and needs a bus reset
  620. * for the changes to take place. When the bus reset is
  621. * detected, the controller loads the new values for the
  622. * ConfigRomHeader and BusOptions registers from the specified
  623. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  624. * shadow register. All automatically and atomically.
  625. *
  626. * Now, there's a twist to this story. The automatic load of
  627. * ConfigRomHeader and BusOptions doesn't honor the
  628. * noByteSwapData bit, so with a be32 config rom, the
  629. * controller will load be32 values in to these registers
  630. * during the atomic update, even on litte endian
  631. * architectures. The workaround we use is to put a 0 in the
  632. * header quadlet; 0 is endian agnostic and means that the
  633. * config rom isn't ready yet. In the bus reset tasklet we
  634. * then set up the real values for the two registers.
  635. *
  636. * We use ohci->lock to avoid racing with the code that sets
  637. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  638. */
  639. next_config_rom =
  640. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  641. &next_config_rom_bus, GFP_KERNEL);
  642. if (next_config_rom == NULL)
  643. return -ENOMEM;
  644. spin_lock_irqsave(&ohci->lock, flags);
  645. if (ohci->next_config_rom == NULL) {
  646. ohci->next_config_rom = next_config_rom;
  647. ohci->next_config_rom_bus = next_config_rom_bus;
  648. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  649. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  650. length * 4);
  651. ohci->next_header = config_rom[0];
  652. ohci->next_config_rom[0] = 0;
  653. reg_write(ohci, OHCI1394_ConfigROMmap,
  654. ohci->next_config_rom_bus);
  655. } else {
  656. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  657. next_config_rom, next_config_rom_bus);
  658. retval = -EBUSY;
  659. }
  660. spin_unlock_irqrestore(&ohci->lock, flags);
  661. /* Now initiate a bus reset to have the changes take
  662. * effect. We clean up the old config rom memory and DMA
  663. * mappings in the bus reset tasklet, since the OHCI
  664. * controller could need to access it before the bus reset
  665. * takes effect. */
  666. if (retval == 0)
  667. fw_core_initiate_bus_reset(&ohci->card, 1);
  668. return retval;
  669. }
  670. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  671. {
  672. struct fw_ohci *ohci = fw_ohci(card);
  673. at_context_transmit(&ohci->at_request_ctx, packet);
  674. }
  675. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  676. {
  677. struct fw_ohci *ohci = fw_ohci(card);
  678. at_context_transmit(&ohci->at_response_ctx, packet);
  679. }
  680. static int
  681. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  682. {
  683. struct fw_ohci *ohci = fw_ohci(card);
  684. unsigned long flags;
  685. int retval = 0;
  686. /* FIXME: make sure this bitmask is cleared when we clear the
  687. * busReset interrupt bit. */
  688. spin_lock_irqsave(&ohci->lock, flags);
  689. if (ohci->generation != generation) {
  690. retval = -ESTALE;
  691. goto out;
  692. }
  693. if (node_id < 32) {
  694. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << node_id);
  695. } else {
  696. reg_write(ohci, OHCI1394_PhyReqFilterHiSet,
  697. 1 << (node_id - 32));
  698. }
  699. flush_writes(ohci);
  700. spin_unlock_irqrestore(&ohci->lock, flags);
  701. out:
  702. return retval;
  703. }
  704. static void ir_context_tasklet(unsigned long data)
  705. {
  706. struct iso_context *ctx = (struct iso_context *)data;
  707. (void)ctx;
  708. }
  709. #define ISO_BUFFER_SIZE (64 * 1024)
  710. static void flush_iso_context(struct iso_context *ctx)
  711. {
  712. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  713. struct descriptor *d, *last;
  714. u32 address;
  715. int z;
  716. dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
  717. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  718. d = ctx->tail_descriptor;
  719. last = ctx->tail_descriptor_last;
  720. while (last->branch_address != 0 && last->transfer_status != 0) {
  721. address = le32_to_cpu(last->branch_address);
  722. z = address & 0xf;
  723. d = ctx->buffer + (address - ctx->buffer_bus) / sizeof *d;
  724. if (z == 2)
  725. last = d;
  726. else
  727. last = d + z - 1;
  728. if (le16_to_cpu(last->control) & descriptor_irq_always)
  729. ctx->base.callback(&ctx->base,
  730. 0, le16_to_cpu(last->res_count),
  731. ctx->base.callback_data);
  732. }
  733. ctx->tail_descriptor = d;
  734. ctx->tail_descriptor_last = last;
  735. }
  736. static void it_context_tasklet(unsigned long data)
  737. {
  738. struct iso_context *ctx = (struct iso_context *)data;
  739. flush_iso_context(ctx);
  740. }
  741. static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
  742. int type)
  743. {
  744. struct fw_ohci *ohci = fw_ohci(card);
  745. struct iso_context *ctx, *list;
  746. void (*tasklet) (unsigned long data);
  747. u32 *mask;
  748. unsigned long flags;
  749. int index;
  750. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  751. mask = &ohci->it_context_mask;
  752. list = ohci->it_context_list;
  753. tasklet = it_context_tasklet;
  754. } else {
  755. mask = &ohci->ir_context_mask;
  756. list = ohci->ir_context_list;
  757. tasklet = ir_context_tasklet;
  758. }
  759. spin_lock_irqsave(&ohci->lock, flags);
  760. index = ffs(*mask) - 1;
  761. if (index >= 0)
  762. *mask &= ~(1 << index);
  763. spin_unlock_irqrestore(&ohci->lock, flags);
  764. if (index < 0)
  765. return ERR_PTR(-EBUSY);
  766. ctx = &list[index];
  767. memset(ctx, 0, sizeof *ctx);
  768. tasklet_init(&ctx->tasklet, tasklet, (unsigned long)ctx);
  769. ctx->buffer = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  770. if (ctx->buffer == NULL) {
  771. spin_lock_irqsave(&ohci->lock, flags);
  772. *mask |= 1 << index;
  773. spin_unlock_irqrestore(&ohci->lock, flags);
  774. return ERR_PTR(-ENOMEM);
  775. }
  776. ctx->buffer_bus =
  777. dma_map_single(card->device, ctx->buffer,
  778. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  779. ctx->head_descriptor = ctx->buffer;
  780. ctx->prev_descriptor = ctx->buffer;
  781. ctx->tail_descriptor = ctx->buffer;
  782. ctx->tail_descriptor_last = ctx->buffer;
  783. /* We put a dummy descriptor in the buffer that has a NULL
  784. * branch address and looks like it's been sent. That way we
  785. * have a descriptor to append DMA programs to. Also, the
  786. * ring buffer invariant is that it always has at least one
  787. * element so that head == tail means buffer full. */
  788. memset(ctx->head_descriptor, 0, sizeof *ctx->head_descriptor);
  789. ctx->head_descriptor->control =
  790. cpu_to_le16(descriptor_output_last);
  791. ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
  792. ctx->head_descriptor++;
  793. return &ctx->base;
  794. }
  795. static int ohci_send_iso(struct fw_iso_context *base, s32 cycle)
  796. {
  797. struct iso_context *ctx = (struct iso_context *)base;
  798. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  799. u32 cycle_match = 0;
  800. int index;
  801. index = ctx - ohci->it_context_list;
  802. if (cycle > 0)
  803. cycle_match = CONTEXT_CYCLE_MATCH_ENABLE |
  804. (cycle & 0x7fff) << 16;
  805. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  806. reg_write(ohci, OHCI1394_IsoXmitCommandPtr(index),
  807. le32_to_cpu(ctx->tail_descriptor_last->branch_address));
  808. reg_write(ohci, OHCI1394_IsoXmitContextControlClear(index), ~0);
  809. reg_write(ohci, OHCI1394_IsoXmitContextControlSet(index),
  810. CONTEXT_RUN | cycle_match);
  811. flush_writes(ohci);
  812. return 0;
  813. }
  814. static void ohci_free_iso_context(struct fw_iso_context *base)
  815. {
  816. struct fw_ohci *ohci = fw_ohci(base->card);
  817. struct iso_context *ctx = (struct iso_context *)base;
  818. unsigned long flags;
  819. int index;
  820. flush_iso_context(ctx);
  821. spin_lock_irqsave(&ohci->lock, flags);
  822. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  823. index = ctx - ohci->it_context_list;
  824. reg_write(ohci, OHCI1394_IsoXmitContextControlClear(index), ~0);
  825. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  826. ohci->it_context_mask |= 1 << index;
  827. } else {
  828. index = ctx - ohci->ir_context_list;
  829. reg_write(ohci, OHCI1394_IsoRcvContextControlClear(index), ~0);
  830. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  831. ohci->ir_context_mask |= 1 << index;
  832. }
  833. flush_writes(ohci);
  834. dma_unmap_single(ohci->card.device, ctx->buffer_bus,
  835. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  836. spin_unlock_irqrestore(&ohci->lock, flags);
  837. }
  838. static int
  839. ohci_queue_iso(struct fw_iso_context *base,
  840. struct fw_iso_packet *packet, void *payload)
  841. {
  842. struct iso_context *ctx = (struct iso_context *)base;
  843. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  844. struct descriptor *d, *end, *last, *tail, *pd;
  845. struct fw_iso_packet *p;
  846. __le32 *header;
  847. dma_addr_t d_bus;
  848. u32 z, header_z, payload_z, irq;
  849. u32 payload_index, payload_end_index, next_page_index;
  850. int index, page, end_page, i, length, offset;
  851. /* FIXME: Cycle lost behavior should be configurable: lose
  852. * packet, retransmit or terminate.. */
  853. p = packet;
  854. payload_index = payload - ctx->base.buffer;
  855. d = ctx->head_descriptor;
  856. tail = ctx->tail_descriptor;
  857. end = ctx->buffer + ISO_BUFFER_SIZE / sizeof(struct descriptor);
  858. if (p->skip)
  859. z = 1;
  860. else
  861. z = 2;
  862. if (p->header_length > 0)
  863. z++;
  864. /* Determine the first page the payload isn't contained in. */
  865. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  866. if (p->payload_length > 0)
  867. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  868. else
  869. payload_z = 0;
  870. z += payload_z;
  871. /* Get header size in number of descriptors. */
  872. header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
  873. if (d + z + header_z <= tail) {
  874. goto has_space;
  875. } else if (d > tail && d + z + header_z <= end) {
  876. goto has_space;
  877. } else if (d > tail && ctx->buffer + z + header_z <= tail) {
  878. d = ctx->buffer;
  879. goto has_space;
  880. }
  881. /* No space in buffer */
  882. return -1;
  883. has_space:
  884. memset(d, 0, (z + header_z) * sizeof *d);
  885. d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
  886. if (!p->skip) {
  887. d[0].control = cpu_to_le16(descriptor_key_immediate);
  888. d[0].req_count = cpu_to_le16(8);
  889. header = (__le32 *) &d[1];
  890. header[0] = cpu_to_le32(it_header_sy(p->sy) |
  891. it_header_tag(p->tag) |
  892. it_header_tcode(TCODE_STREAM_DATA) |
  893. it_header_channel(ctx->base.channel) |
  894. it_header_speed(ctx->base.speed));
  895. header[1] =
  896. cpu_to_le32(it_header_data_length(p->header_length +
  897. p->payload_length));
  898. }
  899. if (p->header_length > 0) {
  900. d[2].req_count = cpu_to_le16(p->header_length);
  901. d[2].data_address = cpu_to_le32(d_bus + z * sizeof *d);
  902. memcpy(&d[z], p->header, p->header_length);
  903. }
  904. pd = d + z - payload_z;
  905. payload_end_index = payload_index + p->payload_length;
  906. for (i = 0; i < payload_z; i++) {
  907. page = payload_index >> PAGE_SHIFT;
  908. offset = payload_index & ~PAGE_MASK;
  909. next_page_index = (page + 1) << PAGE_SHIFT;
  910. length =
  911. min(next_page_index, payload_end_index) - payload_index;
  912. pd[i].req_count = cpu_to_le16(length);
  913. pd[i].data_address = cpu_to_le32(ctx->base.pages[page] + offset);
  914. payload_index += length;
  915. }
  916. if (z == 2)
  917. last = d;
  918. else
  919. last = d + z - 1;
  920. if (p->interrupt)
  921. irq = descriptor_irq_always;
  922. else
  923. irq = descriptor_no_irq;
  924. last->control = cpu_to_le16(descriptor_output_last |
  925. descriptor_status |
  926. descriptor_branch_always |
  927. irq);
  928. dma_sync_single_for_device(ohci->card.device, ctx->buffer_bus,
  929. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  930. ctx->head_descriptor = d + z + header_z;
  931. ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
  932. ctx->prev_descriptor = last;
  933. index = ctx - ohci->it_context_list;
  934. reg_write(ohci, OHCI1394_IsoXmitContextControlSet(index), CONTEXT_WAKE);
  935. flush_writes(ohci);
  936. return 0;
  937. }
  938. static struct fw_card_driver ohci_driver = {
  939. .name = ohci_driver_name,
  940. .enable = ohci_enable,
  941. .update_phy_reg = ohci_update_phy_reg,
  942. .set_config_rom = ohci_set_config_rom,
  943. .send_request = ohci_send_request,
  944. .send_response = ohci_send_response,
  945. .enable_phys_dma = ohci_enable_phys_dma,
  946. .allocate_iso_context = ohci_allocate_iso_context,
  947. .free_iso_context = ohci_free_iso_context,
  948. .queue_iso = ohci_queue_iso,
  949. .send_iso = ohci_send_iso
  950. };
  951. static int software_reset(struct fw_ohci *ohci)
  952. {
  953. int i;
  954. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  955. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  956. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  957. OHCI1394_HCControl_softReset) == 0)
  958. return 0;
  959. msleep(1);
  960. }
  961. return -EBUSY;
  962. }
  963. /* ---------- pci subsystem interface ---------- */
  964. enum {
  965. CLEANUP_SELF_ID,
  966. CLEANUP_REGISTERS,
  967. CLEANUP_IOMEM,
  968. CLEANUP_DISABLE,
  969. CLEANUP_PUT_CARD,
  970. };
  971. static int cleanup(struct fw_ohci *ohci, int stage, int code)
  972. {
  973. struct pci_dev *dev = to_pci_dev(ohci->card.device);
  974. switch (stage) {
  975. case CLEANUP_SELF_ID:
  976. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  977. ohci->self_id_cpu, ohci->self_id_bus);
  978. case CLEANUP_REGISTERS:
  979. kfree(ohci->it_context_list);
  980. kfree(ohci->ir_context_list);
  981. pci_iounmap(dev, ohci->registers);
  982. case CLEANUP_IOMEM:
  983. pci_release_region(dev, 0);
  984. case CLEANUP_DISABLE:
  985. pci_disable_device(dev);
  986. case CLEANUP_PUT_CARD:
  987. fw_card_put(&ohci->card);
  988. }
  989. return code;
  990. }
  991. static int __devinit
  992. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  993. {
  994. struct fw_ohci *ohci;
  995. u32 bus_options, max_receive, link_speed;
  996. u64 guid;
  997. int error_code;
  998. size_t size;
  999. ohci = kzalloc(sizeof *ohci, GFP_KERNEL);
  1000. if (ohci == NULL) {
  1001. fw_error("Could not malloc fw_ohci data.\n");
  1002. return -ENOMEM;
  1003. }
  1004. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1005. if (pci_enable_device(dev)) {
  1006. fw_error("Failed to enable OHCI hardware.\n");
  1007. return cleanup(ohci, CLEANUP_PUT_CARD, -ENODEV);
  1008. }
  1009. pci_set_master(dev);
  1010. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1011. pci_set_drvdata(dev, ohci);
  1012. spin_lock_init(&ohci->lock);
  1013. tasklet_init(&ohci->bus_reset_tasklet,
  1014. bus_reset_tasklet, (unsigned long)ohci);
  1015. if (pci_request_region(dev, 0, ohci_driver_name)) {
  1016. fw_error("MMIO resource unavailable\n");
  1017. return cleanup(ohci, CLEANUP_DISABLE, -EBUSY);
  1018. }
  1019. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1020. if (ohci->registers == NULL) {
  1021. fw_error("Failed to remap registers\n");
  1022. return cleanup(ohci, CLEANUP_IOMEM, -ENXIO);
  1023. }
  1024. if (software_reset(ohci)) {
  1025. fw_error("Failed to reset ohci card.\n");
  1026. return cleanup(ohci, CLEANUP_REGISTERS, -EBUSY);
  1027. }
  1028. /* Now enable LPS, which we need in order to start accessing
  1029. * most of the registers. In fact, on some cards (ALI M5251),
  1030. * accessing registers in the SClk domain without LPS enabled
  1031. * will lock up the machine. Wait 50msec to make sure we have
  1032. * full link enabled. */
  1033. reg_write(ohci, OHCI1394_HCControlSet,
  1034. OHCI1394_HCControl_LPS |
  1035. OHCI1394_HCControl_postedWriteEnable);
  1036. flush_writes(ohci);
  1037. msleep(50);
  1038. reg_write(ohci, OHCI1394_HCControlClear,
  1039. OHCI1394_HCControl_noByteSwapData);
  1040. reg_write(ohci, OHCI1394_LinkControlSet,
  1041. OHCI1394_LinkControl_rcvSelfID |
  1042. OHCI1394_LinkControl_cycleTimerEnable |
  1043. OHCI1394_LinkControl_cycleMaster);
  1044. ar_context_init(&ohci->ar_request_ctx, ohci,
  1045. OHCI1394_AsReqRcvContextControlSet);
  1046. ar_context_init(&ohci->ar_response_ctx, ohci,
  1047. OHCI1394_AsRspRcvContextControlSet);
  1048. at_context_init(&ohci->at_request_ctx, ohci,
  1049. OHCI1394_AsReqTrContextControlSet);
  1050. at_context_init(&ohci->at_response_ctx, ohci,
  1051. OHCI1394_AsRspTrContextControlSet);
  1052. reg_write(ohci, OHCI1394_ATRetries,
  1053. OHCI1394_MAX_AT_REQ_RETRIES |
  1054. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1055. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1056. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1057. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1058. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1059. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1060. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1061. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1062. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1063. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1064. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1065. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1066. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1067. fw_error("Out of memory for it/ir contexts.\n");
  1068. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1069. }
  1070. /* self-id dma buffer allocation */
  1071. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1072. SELF_ID_BUF_SIZE,
  1073. &ohci->self_id_bus,
  1074. GFP_KERNEL);
  1075. if (ohci->self_id_cpu == NULL) {
  1076. fw_error("Out of memory for self ID buffer.\n");
  1077. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1078. }
  1079. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1080. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1081. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1082. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1083. reg_write(ohci, OHCI1394_IntMaskSet,
  1084. OHCI1394_selfIDComplete |
  1085. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1086. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1087. OHCI1394_isochRx | OHCI1394_isochTx |
  1088. OHCI1394_masterIntEnable);
  1089. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1090. max_receive = (bus_options >> 12) & 0xf;
  1091. link_speed = bus_options & 0x7;
  1092. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1093. reg_read(ohci, OHCI1394_GUIDLo);
  1094. error_code = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1095. if (error_code < 0)
  1096. return cleanup(ohci, CLEANUP_SELF_ID, error_code);
  1097. fw_notify("Added fw-ohci device %s.\n", dev->dev.bus_id);
  1098. return 0;
  1099. }
  1100. static void pci_remove(struct pci_dev *dev)
  1101. {
  1102. struct fw_ohci *ohci;
  1103. ohci = pci_get_drvdata(dev);
  1104. reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_masterIntEnable);
  1105. fw_core_remove_card(&ohci->card);
  1106. /* FIXME: Fail all pending packets here, now that the upper
  1107. * layers can't queue any more. */
  1108. software_reset(ohci);
  1109. free_irq(dev->irq, ohci);
  1110. cleanup(ohci, CLEANUP_SELF_ID, 0);
  1111. fw_notify("Removed fw-ohci device.\n");
  1112. }
  1113. static struct pci_device_id pci_table[] = {
  1114. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1115. { }
  1116. };
  1117. MODULE_DEVICE_TABLE(pci, pci_table);
  1118. static struct pci_driver fw_ohci_pci_driver = {
  1119. .name = ohci_driver_name,
  1120. .id_table = pci_table,
  1121. .probe = pci_probe,
  1122. .remove = pci_remove,
  1123. };
  1124. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1125. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1126. MODULE_LICENSE("GPL");
  1127. static int __init fw_ohci_init(void)
  1128. {
  1129. return pci_register_driver(&fw_ohci_pci_driver);
  1130. }
  1131. static void __exit fw_ohci_cleanup(void)
  1132. {
  1133. pci_unregister_driver(&fw_ohci_pci_driver);
  1134. }
  1135. module_init(fw_ohci_init);
  1136. module_exit(fw_ohci_cleanup);