fw-ohci.c 40 KB

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