fw-ohci.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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. /* FIXME: Move this to linux/pci_ids.h */
  144. #define PCI_CLASS_SERIAL_FIREWIRE_OHCI 0x0c0010
  145. static char ohci_driver_name[] = KBUILD_MODNAME;
  146. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  147. {
  148. writel(data, ohci->registers + offset);
  149. }
  150. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  151. {
  152. return readl(ohci->registers + offset);
  153. }
  154. static inline void flush_writes(const struct fw_ohci *ohci)
  155. {
  156. /* Do a dummy read to flush writes. */
  157. reg_read(ohci, OHCI1394_Version);
  158. }
  159. static int
  160. ohci_update_phy_reg(struct fw_card *card, int addr,
  161. int clear_bits, int set_bits)
  162. {
  163. struct fw_ohci *ohci = fw_ohci(card);
  164. u32 val, old;
  165. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  166. msleep(2);
  167. val = reg_read(ohci, OHCI1394_PhyControl);
  168. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  169. fw_error("failed to set phy reg bits.\n");
  170. return -EBUSY;
  171. }
  172. old = OHCI1394_PhyControl_ReadData(val);
  173. old = (old & ~clear_bits) | set_bits;
  174. reg_write(ohci, OHCI1394_PhyControl,
  175. OHCI1394_PhyControl_Write(addr, old));
  176. return 0;
  177. }
  178. static void ar_context_run(struct ar_context *ctx)
  179. {
  180. reg_write(ctx->ohci, ctx->command_ptr, ctx->descriptor_bus | 1);
  181. reg_write(ctx->ohci, ctx->control_set, CONTEXT_RUN);
  182. flush_writes(ctx->ohci);
  183. }
  184. static void ar_context_tasklet(unsigned long data)
  185. {
  186. struct ar_context *ctx = (struct ar_context *)data;
  187. struct fw_ohci *ohci = ctx->ohci;
  188. u32 status;
  189. int length, speed, ack, timestamp, tcode;
  190. /* FIXME: What to do about evt_* errors? */
  191. length = le16_to_cpu(ctx->descriptor.req_count) -
  192. le16_to_cpu(ctx->descriptor.res_count) - 4;
  193. status = le32_to_cpu(ctx->buffer[length / 4]);
  194. ack = ((status >> 16) & 0x1f) - 16;
  195. speed = (status >> 21) & 0x7;
  196. timestamp = status & 0xffff;
  197. ctx->buffer[0] = le32_to_cpu(ctx->buffer[0]);
  198. ctx->buffer[1] = le32_to_cpu(ctx->buffer[1]);
  199. ctx->buffer[2] = le32_to_cpu(ctx->buffer[2]);
  200. tcode = (ctx->buffer[0] >> 4) & 0x0f;
  201. if (TCODE_IS_BLOCK_PACKET(tcode))
  202. ctx->buffer[3] = le32_to_cpu(ctx->buffer[3]);
  203. /* The OHCI bus reset handler synthesizes a phy packet with
  204. * the new generation number when a bus reset happens (see
  205. * section 8.4.2.3). This helps us determine when a request
  206. * was received and make sure we send the response in the same
  207. * generation. We only need this for requests; for responses
  208. * we use the unique tlabel for finding the matching
  209. * request. */
  210. if (ack + 16 == 0x09)
  211. ohci->request_generation = (ctx->buffer[2] >> 16) & 0xff;
  212. else if (ctx == &ohci->ar_request_ctx)
  213. fw_core_handle_request(&ohci->card, speed, ack, timestamp,
  214. ohci->request_generation,
  215. length, ctx->buffer);
  216. else
  217. fw_core_handle_response(&ohci->card, speed, ack, timestamp,
  218. length, ctx->buffer);
  219. ctx->descriptor.data_address = cpu_to_le32(ctx->buffer_bus);
  220. ctx->descriptor.req_count = cpu_to_le16(sizeof ctx->buffer);
  221. ctx->descriptor.res_count = cpu_to_le16(sizeof ctx->buffer);
  222. dma_sync_single_for_device(ohci->card.device, ctx->descriptor_bus,
  223. sizeof ctx->descriptor_bus, DMA_TO_DEVICE);
  224. /* FIXME: We stop and restart the ar context here, what if we
  225. * stop while a receive is in progress? Maybe we could just
  226. * loop the context back to itself and use it in buffer fill
  227. * mode as intended... */
  228. reg_write(ctx->ohci, ctx->control_clear, CONTEXT_RUN);
  229. ar_context_run(ctx);
  230. }
  231. static int
  232. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 control_set)
  233. {
  234. ctx->descriptor_bus =
  235. dma_map_single(ohci->card.device, &ctx->descriptor,
  236. sizeof ctx->descriptor, DMA_TO_DEVICE);
  237. if (ctx->descriptor_bus == 0)
  238. return -ENOMEM;
  239. if (ctx->descriptor_bus & 0xf)
  240. fw_notify("descriptor not 16-byte aligned: 0x%08lx\n",
  241. (unsigned long)ctx->descriptor_bus);
  242. ctx->buffer_bus =
  243. dma_map_single(ohci->card.device, ctx->buffer,
  244. sizeof ctx->buffer, DMA_FROM_DEVICE);
  245. if (ctx->buffer_bus == 0) {
  246. dma_unmap_single(ohci->card.device, ctx->descriptor_bus,
  247. sizeof ctx->descriptor, DMA_TO_DEVICE);
  248. return -ENOMEM;
  249. }
  250. memset(&ctx->descriptor, 0, sizeof ctx->descriptor);
  251. ctx->descriptor.control = cpu_to_le16(descriptor_input_more |
  252. descriptor_status |
  253. descriptor_branch_always);
  254. ctx->descriptor.req_count = cpu_to_le16(sizeof ctx->buffer);
  255. ctx->descriptor.data_address = cpu_to_le32(ctx->buffer_bus);
  256. ctx->descriptor.res_count = cpu_to_le16(sizeof ctx->buffer);
  257. ctx->control_set = control_set;
  258. ctx->control_clear = control_set + 4;
  259. ctx->command_ptr = control_set + 12;
  260. ctx->ohci = ohci;
  261. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  262. ar_context_run(ctx);
  263. return 0;
  264. }
  265. static void
  266. do_packet_callbacks(struct fw_ohci *ohci, struct list_head *list)
  267. {
  268. struct fw_packet *p, *next;
  269. list_for_each_entry_safe(p, next, list, link)
  270. p->callback(p, &ohci->card, p->status);
  271. }
  272. static void
  273. complete_transmission(struct fw_packet *packet,
  274. int status, struct list_head *list)
  275. {
  276. list_move_tail(&packet->link, list);
  277. packet->status = status;
  278. }
  279. /* This function prepares the first packet in the context queue for
  280. * transmission. Must always be called with the ochi->lock held to
  281. * ensure proper generation handling and locking around packet queue
  282. * manipulation. */
  283. static void
  284. at_context_setup_packet(struct at_context *ctx, struct list_head *list)
  285. {
  286. struct fw_packet *packet;
  287. struct fw_ohci *ohci = ctx->ohci;
  288. int z, tcode;
  289. packet = fw_packet(ctx->list.next);
  290. memset(&ctx->d, 0, sizeof ctx->d);
  291. if (packet->payload_length > 0) {
  292. packet->payload_bus = dma_map_single(ohci->card.device,
  293. packet->payload,
  294. packet->payload_length,
  295. DMA_TO_DEVICE);
  296. if (packet->payload_bus == 0) {
  297. complete_transmission(packet, -ENOMEM, list);
  298. return;
  299. }
  300. ctx->d.more.control =
  301. cpu_to_le16(descriptor_output_more |
  302. descriptor_key_immediate);
  303. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  304. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  305. ctx->d.last.control =
  306. cpu_to_le16(descriptor_output_last |
  307. descriptor_irq_always |
  308. descriptor_branch_always);
  309. ctx->d.last.req_count = cpu_to_le16(packet->payload_length);
  310. ctx->d.last.data_address = cpu_to_le32(packet->payload_bus);
  311. z = 3;
  312. } else {
  313. ctx->d.more.control =
  314. cpu_to_le16(descriptor_output_last |
  315. descriptor_key_immediate |
  316. descriptor_irq_always |
  317. descriptor_branch_always);
  318. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  319. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  320. z = 2;
  321. }
  322. /* The DMA format for asyncronous link packets is different
  323. * from the IEEE1394 layout, so shift the fields around
  324. * accordingly. If header_length is 8, it's a PHY packet, to
  325. * which we need to prepend an extra quadlet. */
  326. if (packet->header_length > 8) {
  327. ctx->d.header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  328. (packet->speed << 16));
  329. ctx->d.header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  330. (packet->header[0] & 0xffff0000));
  331. ctx->d.header[2] = cpu_to_le32(packet->header[2]);
  332. tcode = (packet->header[0] >> 4) & 0x0f;
  333. if (TCODE_IS_BLOCK_PACKET(tcode))
  334. ctx->d.header[3] = cpu_to_le32(packet->header[3]);
  335. else
  336. ctx->d.header[3] = packet->header[3];
  337. } else {
  338. ctx->d.header[0] =
  339. cpu_to_le32((OHCI1394_phy_tcode << 4) |
  340. (packet->speed << 16));
  341. ctx->d.header[1] = cpu_to_le32(packet->header[0]);
  342. ctx->d.header[2] = cpu_to_le32(packet->header[1]);
  343. ctx->d.more.req_count = cpu_to_le16(12);
  344. }
  345. /* FIXME: Document how the locking works. */
  346. if (ohci->generation == packet->generation) {
  347. reg_write(ctx->ohci, ctx->command_ptr,
  348. ctx->descriptor_bus | z);
  349. reg_write(ctx->ohci, ctx->control_set,
  350. CONTEXT_RUN | CONTEXT_WAKE);
  351. } else {
  352. /* We dont return error codes from this function; all
  353. * transmission errors are reported through the
  354. * callback. */
  355. complete_transmission(packet, -ESTALE, list);
  356. }
  357. }
  358. static void at_context_stop(struct at_context *ctx)
  359. {
  360. u32 reg;
  361. reg_write(ctx->ohci, ctx->control_clear, CONTEXT_RUN);
  362. reg = reg_read(ctx->ohci, ctx->control_set);
  363. if (reg & CONTEXT_ACTIVE)
  364. fw_notify("Tried to stop context, but it is still active "
  365. "(0x%08x).\n", reg);
  366. }
  367. static void at_context_tasklet(unsigned long data)
  368. {
  369. struct at_context *ctx = (struct at_context *)data;
  370. struct fw_ohci *ohci = ctx->ohci;
  371. struct fw_packet *packet;
  372. LIST_HEAD(list);
  373. unsigned long flags;
  374. int evt;
  375. spin_lock_irqsave(&ohci->lock, flags);
  376. packet = fw_packet(ctx->list.next);
  377. at_context_stop(ctx);
  378. if (packet->payload_length > 0) {
  379. dma_unmap_single(ohci->card.device, packet->payload_bus,
  380. packet->payload_length, DMA_TO_DEVICE);
  381. evt = le16_to_cpu(ctx->d.last.transfer_status) & 0x1f;
  382. packet->timestamp = le16_to_cpu(ctx->d.last.res_count);
  383. }
  384. else {
  385. evt = le16_to_cpu(ctx->d.more.transfer_status) & 0x1f;
  386. packet->timestamp = le16_to_cpu(ctx->d.more.res_count);
  387. }
  388. if (evt < 16) {
  389. switch (evt) {
  390. case OHCI1394_evt_timeout:
  391. /* Async response transmit timed out. */
  392. complete_transmission(packet, -ETIMEDOUT, &list);
  393. break;
  394. case OHCI1394_evt_flushed:
  395. /* The packet was flushed should give same
  396. * error as when we try to use a stale
  397. * generation count. */
  398. complete_transmission(packet, -ESTALE, &list);
  399. break;
  400. case OHCI1394_evt_missing_ack:
  401. /* This would be a higher level software
  402. * error, it is using a valid (current)
  403. * generation count, but the node is not on
  404. * the bus. */
  405. complete_transmission(packet, -ENODEV, &list);
  406. break;
  407. default:
  408. complete_transmission(packet, -EIO, &list);
  409. break;
  410. }
  411. } else
  412. complete_transmission(packet, evt - 16, &list);
  413. /* If more packets are queued, set up the next one. */
  414. if (!list_empty(&ctx->list))
  415. at_context_setup_packet(ctx, &list);
  416. spin_unlock_irqrestore(&ohci->lock, flags);
  417. do_packet_callbacks(ohci, &list);
  418. }
  419. static int
  420. at_context_init(struct at_context *ctx, struct fw_ohci *ohci, u32 control_set)
  421. {
  422. INIT_LIST_HEAD(&ctx->list);
  423. ctx->descriptor_bus =
  424. dma_map_single(ohci->card.device, &ctx->d,
  425. sizeof ctx->d, DMA_TO_DEVICE);
  426. if (ctx->descriptor_bus == 0)
  427. return -ENOMEM;
  428. ctx->control_set = control_set;
  429. ctx->control_clear = control_set + 4;
  430. ctx->command_ptr = control_set + 12;
  431. ctx->ohci = ohci;
  432. tasklet_init(&ctx->tasklet, at_context_tasklet, (unsigned long)ctx);
  433. return 0;
  434. }
  435. static void
  436. at_context_transmit(struct at_context *ctx, struct fw_packet *packet)
  437. {
  438. LIST_HEAD(list);
  439. unsigned long flags;
  440. int was_empty;
  441. spin_lock_irqsave(&ctx->ohci->lock, flags);
  442. was_empty = list_empty(&ctx->list);
  443. list_add_tail(&packet->link, &ctx->list);
  444. if (was_empty)
  445. at_context_setup_packet(ctx, &list);
  446. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  447. do_packet_callbacks(ctx->ohci, &list);
  448. }
  449. static void bus_reset_tasklet(unsigned long data)
  450. {
  451. struct fw_ohci *ohci = (struct fw_ohci *)data;
  452. int self_id_count, i, j, reg, node_id;
  453. int generation, new_generation;
  454. unsigned long flags;
  455. reg = reg_read(ohci, OHCI1394_NodeID);
  456. if (!(reg & OHCI1394_NodeID_idValid)) {
  457. fw_error("node ID not valid, new bus reset in progress\n");
  458. return;
  459. }
  460. node_id = reg & 0xffff;
  461. /* The count in the SelfIDCount register is the number of
  462. * bytes in the self ID receive buffer. Since we also receive
  463. * the inverted quadlets and a header quadlet, we shift one
  464. * bit extra to get the actual number of self IDs. */
  465. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  466. generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  467. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  468. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  469. fw_error("inconsistent self IDs\n");
  470. ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
  471. }
  472. /* Check the consistency of the self IDs we just read. The
  473. * problem we face is that a new bus reset can start while we
  474. * read out the self IDs from the DMA buffer. If this happens,
  475. * the DMA buffer will be overwritten with new self IDs and we
  476. * will read out inconsistent data. The OHCI specification
  477. * (section 11.2) recommends a technique similar to
  478. * linux/seqlock.h, where we remember the generation of the
  479. * self IDs in the buffer before reading them out and compare
  480. * it to the current generation after reading them out. If
  481. * the two generations match we know we have a consistent set
  482. * of self IDs. */
  483. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  484. if (new_generation != generation) {
  485. fw_notify("recursive bus reset detected, "
  486. "discarding self ids\n");
  487. return;
  488. }
  489. /* FIXME: Document how the locking works. */
  490. spin_lock_irqsave(&ohci->lock, flags);
  491. ohci->generation = generation;
  492. at_context_stop(&ohci->at_request_ctx);
  493. at_context_stop(&ohci->at_response_ctx);
  494. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  495. /* This next bit is unrelated to the AT context stuff but we
  496. * have to do it under the spinlock also. If a new config rom
  497. * was set up before this reset, the old one is now no longer
  498. * in use and we can free it. Update the config rom pointers
  499. * to point to the current config rom and clear the
  500. * next_config_rom pointer so a new udpate can take place. */
  501. if (ohci->next_config_rom != NULL) {
  502. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  503. ohci->config_rom, ohci->config_rom_bus);
  504. ohci->config_rom = ohci->next_config_rom;
  505. ohci->config_rom_bus = ohci->next_config_rom_bus;
  506. ohci->next_config_rom = NULL;
  507. /* Restore config_rom image and manually update
  508. * config_rom registers. Writing the header quadlet
  509. * will indicate that the config rom is ready, so we
  510. * do that last. */
  511. reg_write(ohci, OHCI1394_BusOptions,
  512. be32_to_cpu(ohci->config_rom[2]));
  513. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  514. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  515. }
  516. spin_unlock_irqrestore(&ohci->lock, flags);
  517. fw_core_handle_bus_reset(&ohci->card, node_id, generation,
  518. self_id_count, ohci->self_id_buffer);
  519. }
  520. static irqreturn_t irq_handler(int irq, void *data)
  521. {
  522. struct fw_ohci *ohci = data;
  523. u32 event, iso_event;
  524. int i;
  525. event = reg_read(ohci, OHCI1394_IntEventClear);
  526. if (!event)
  527. return IRQ_NONE;
  528. reg_write(ohci, OHCI1394_IntEventClear, event);
  529. if (event & OHCI1394_selfIDComplete)
  530. tasklet_schedule(&ohci->bus_reset_tasklet);
  531. if (event & OHCI1394_RQPkt)
  532. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  533. if (event & OHCI1394_RSPkt)
  534. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  535. if (event & OHCI1394_reqTxComplete)
  536. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  537. if (event & OHCI1394_respTxComplete)
  538. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  539. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventSet);
  540. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  541. while (iso_event) {
  542. i = ffs(iso_event) - 1;
  543. tasklet_schedule(&ohci->ir_context_list[i].tasklet);
  544. iso_event &= ~(1 << i);
  545. }
  546. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventSet);
  547. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  548. while (iso_event) {
  549. i = ffs(iso_event) - 1;
  550. tasklet_schedule(&ohci->it_context_list[i].tasklet);
  551. iso_event &= ~(1 << i);
  552. }
  553. return IRQ_HANDLED;
  554. }
  555. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  556. {
  557. struct fw_ohci *ohci = fw_ohci(card);
  558. struct pci_dev *dev = to_pci_dev(card->device);
  559. /* When the link is not yet enabled, the atomic config rom
  560. * update mechanism described below in ohci_set_config_rom()
  561. * is not active. We have to update ConfigRomHeader and
  562. * BusOptions manually, and the write to ConfigROMmap takes
  563. * effect immediately. We tie this to the enabling of the
  564. * link, so we have a valid config rom before enabling - the
  565. * OHCI requires that ConfigROMhdr and BusOptions have valid
  566. * values before enabling.
  567. *
  568. * However, when the ConfigROMmap is written, some controllers
  569. * always read back quadlets 0 and 2 from the config rom to
  570. * the ConfigRomHeader and BusOptions registers on bus reset.
  571. * They shouldn't do that in this initial case where the link
  572. * isn't enabled. This means we have to use the same
  573. * workaround here, setting the bus header to 0 and then write
  574. * the right values in the bus reset tasklet.
  575. */
  576. ohci->next_config_rom =
  577. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  578. &ohci->next_config_rom_bus, GFP_KERNEL);
  579. if (ohci->next_config_rom == NULL)
  580. return -ENOMEM;
  581. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  582. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  583. ohci->next_header = config_rom[0];
  584. ohci->next_config_rom[0] = 0;
  585. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  586. reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
  587. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  588. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  589. if (request_irq(dev->irq, irq_handler,
  590. SA_SHIRQ, ohci_driver_name, ohci)) {
  591. fw_error("Failed to allocate shared interrupt %d.\n",
  592. dev->irq);
  593. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  594. ohci->config_rom, ohci->config_rom_bus);
  595. return -EIO;
  596. }
  597. reg_write(ohci, OHCI1394_HCControlSet,
  598. OHCI1394_HCControl_linkEnable |
  599. OHCI1394_HCControl_BIBimageValid);
  600. flush_writes(ohci);
  601. /* We are ready to go, initiate bus reset to finish the
  602. * initialization. */
  603. fw_core_initiate_bus_reset(&ohci->card, 1);
  604. return 0;
  605. }
  606. static int
  607. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  608. {
  609. struct fw_ohci *ohci;
  610. unsigned long flags;
  611. int retval = 0;
  612. __be32 *next_config_rom;
  613. dma_addr_t next_config_rom_bus;
  614. ohci = fw_ohci(card);
  615. /* When the OHCI controller is enabled, the config rom update
  616. * mechanism is a bit tricky, but easy enough to use. See
  617. * section 5.5.6 in the OHCI specification.
  618. *
  619. * The OHCI controller caches the new config rom address in a
  620. * shadow register (ConfigROMmapNext) and needs a bus reset
  621. * for the changes to take place. When the bus reset is
  622. * detected, the controller loads the new values for the
  623. * ConfigRomHeader and BusOptions registers from the specified
  624. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  625. * shadow register. All automatically and atomically.
  626. *
  627. * Now, there's a twist to this story. The automatic load of
  628. * ConfigRomHeader and BusOptions doesn't honor the
  629. * noByteSwapData bit, so with a be32 config rom, the
  630. * controller will load be32 values in to these registers
  631. * during the atomic update, even on litte endian
  632. * architectures. The workaround we use is to put a 0 in the
  633. * header quadlet; 0 is endian agnostic and means that the
  634. * config rom isn't ready yet. In the bus reset tasklet we
  635. * then set up the real values for the two registers.
  636. *
  637. * We use ohci->lock to avoid racing with the code that sets
  638. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  639. */
  640. next_config_rom =
  641. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  642. &next_config_rom_bus, GFP_KERNEL);
  643. if (next_config_rom == NULL)
  644. return -ENOMEM;
  645. spin_lock_irqsave(&ohci->lock, flags);
  646. if (ohci->next_config_rom == NULL) {
  647. ohci->next_config_rom = next_config_rom;
  648. ohci->next_config_rom_bus = next_config_rom_bus;
  649. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  650. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  651. length * 4);
  652. ohci->next_header = config_rom[0];
  653. ohci->next_config_rom[0] = 0;
  654. reg_write(ohci, OHCI1394_ConfigROMmap,
  655. ohci->next_config_rom_bus);
  656. } else {
  657. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  658. next_config_rom, next_config_rom_bus);
  659. retval = -EBUSY;
  660. }
  661. spin_unlock_irqrestore(&ohci->lock, flags);
  662. /* Now initiate a bus reset to have the changes take
  663. * effect. We clean up the old config rom memory and DMA
  664. * mappings in the bus reset tasklet, since the OHCI
  665. * controller could need to access it before the bus reset
  666. * takes effect. */
  667. if (retval == 0)
  668. fw_core_initiate_bus_reset(&ohci->card, 1);
  669. return retval;
  670. }
  671. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  672. {
  673. struct fw_ohci *ohci = fw_ohci(card);
  674. at_context_transmit(&ohci->at_request_ctx, packet);
  675. }
  676. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  677. {
  678. struct fw_ohci *ohci = fw_ohci(card);
  679. at_context_transmit(&ohci->at_response_ctx, packet);
  680. }
  681. static int
  682. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  683. {
  684. struct fw_ohci *ohci = fw_ohci(card);
  685. unsigned long flags;
  686. int retval = 0;
  687. /* FIXME: make sure this bitmask is cleared when we clear the
  688. * busReset interrupt bit. */
  689. spin_lock_irqsave(&ohci->lock, flags);
  690. if (ohci->generation != generation) {
  691. retval = -ESTALE;
  692. goto out;
  693. }
  694. if (node_id < 32) {
  695. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << node_id);
  696. } else {
  697. reg_write(ohci, OHCI1394_PhyReqFilterHiSet,
  698. 1 << (node_id - 32));
  699. }
  700. flush_writes(ohci);
  701. spin_unlock_irqrestore(&ohci->lock, flags);
  702. out:
  703. return retval;
  704. }
  705. static void ir_context_tasklet(unsigned long data)
  706. {
  707. struct iso_context *ctx = (struct iso_context *)data;
  708. (void)ctx;
  709. }
  710. #define ISO_BUFFER_SIZE (64 * 1024)
  711. static void flush_iso_context(struct iso_context *ctx)
  712. {
  713. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  714. struct descriptor *d, *last;
  715. u32 address;
  716. int z;
  717. dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
  718. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  719. d = ctx->tail_descriptor;
  720. last = ctx->tail_descriptor_last;
  721. while (last->branch_address != 0 && last->transfer_status != 0) {
  722. address = le32_to_cpu(last->branch_address);
  723. z = address & 0xf;
  724. d = ctx->buffer + (address - ctx->buffer_bus) / sizeof *d;
  725. if (z == 2)
  726. last = d;
  727. else
  728. last = d + z - 1;
  729. if (le16_to_cpu(last->control) & descriptor_irq_always)
  730. ctx->base.callback(&ctx->base,
  731. 0, le16_to_cpu(last->res_count),
  732. ctx->base.callback_data);
  733. }
  734. ctx->tail_descriptor = d;
  735. ctx->tail_descriptor_last = last;
  736. }
  737. static void it_context_tasklet(unsigned long data)
  738. {
  739. struct iso_context *ctx = (struct iso_context *)data;
  740. flush_iso_context(ctx);
  741. }
  742. static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
  743. int type)
  744. {
  745. struct fw_ohci *ohci = fw_ohci(card);
  746. struct iso_context *ctx, *list;
  747. void (*tasklet) (unsigned long data);
  748. u32 *mask;
  749. unsigned long flags;
  750. int index;
  751. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  752. mask = &ohci->it_context_mask;
  753. list = ohci->it_context_list;
  754. tasklet = it_context_tasklet;
  755. } else {
  756. mask = &ohci->ir_context_mask;
  757. list = ohci->ir_context_list;
  758. tasklet = ir_context_tasklet;
  759. }
  760. spin_lock_irqsave(&ohci->lock, flags);
  761. index = ffs(*mask) - 1;
  762. if (index >= 0)
  763. *mask &= ~(1 << index);
  764. spin_unlock_irqrestore(&ohci->lock, flags);
  765. if (index < 0)
  766. return ERR_PTR(-EBUSY);
  767. ctx = &list[index];
  768. memset(ctx, 0, sizeof *ctx);
  769. tasklet_init(&ctx->tasklet, tasklet, (unsigned long)ctx);
  770. ctx->buffer = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  771. if (ctx->buffer == NULL) {
  772. spin_lock_irqsave(&ohci->lock, flags);
  773. *mask |= 1 << index;
  774. spin_unlock_irqrestore(&ohci->lock, flags);
  775. return ERR_PTR(-ENOMEM);
  776. }
  777. ctx->buffer_bus =
  778. dma_map_single(card->device, ctx->buffer,
  779. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  780. ctx->head_descriptor = ctx->buffer;
  781. ctx->prev_descriptor = ctx->buffer;
  782. ctx->tail_descriptor = ctx->buffer;
  783. ctx->tail_descriptor_last = ctx->buffer;
  784. /* We put a dummy descriptor in the buffer that has a NULL
  785. * branch address and looks like it's been sent. That way we
  786. * have a descriptor to append DMA programs to. Also, the
  787. * ring buffer invariant is that it always has at least one
  788. * element so that head == tail means buffer full. */
  789. memset(ctx->head_descriptor, 0, sizeof *ctx->head_descriptor);
  790. ctx->head_descriptor->control = 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 const 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);