fw-ohci.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. /*
  2. * Driver for OHCI 1394 controllers
  3. *
  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 <linux/mm.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/semaphore.h>
  31. #include "fw-transaction.h"
  32. #include "fw-ohci.h"
  33. #define DESCRIPTOR_OUTPUT_MORE 0
  34. #define DESCRIPTOR_OUTPUT_LAST (1 << 12)
  35. #define DESCRIPTOR_INPUT_MORE (2 << 12)
  36. #define DESCRIPTOR_INPUT_LAST (3 << 12)
  37. #define DESCRIPTOR_STATUS (1 << 11)
  38. #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
  39. #define DESCRIPTOR_PING (1 << 7)
  40. #define DESCRIPTOR_YY (1 << 6)
  41. #define DESCRIPTOR_NO_IRQ (0 << 4)
  42. #define DESCRIPTOR_IRQ_ERROR (1 << 4)
  43. #define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
  44. #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
  45. #define DESCRIPTOR_WAIT (3 << 0)
  46. struct descriptor {
  47. __le16 req_count;
  48. __le16 control;
  49. __le32 data_address;
  50. __le32 branch_address;
  51. __le16 res_count;
  52. __le16 transfer_status;
  53. } __attribute__((aligned(16)));
  54. struct db_descriptor {
  55. __le16 first_size;
  56. __le16 control;
  57. __le16 second_req_count;
  58. __le16 first_req_count;
  59. __le32 branch_address;
  60. __le16 second_res_count;
  61. __le16 first_res_count;
  62. __le32 reserved0;
  63. __le32 first_buffer;
  64. __le32 second_buffer;
  65. __le32 reserved1;
  66. } __attribute__((aligned(16)));
  67. #define CONTROL_SET(regs) (regs)
  68. #define CONTROL_CLEAR(regs) ((regs) + 4)
  69. #define COMMAND_PTR(regs) ((regs) + 12)
  70. #define CONTEXT_MATCH(regs) ((regs) + 16)
  71. struct ar_buffer {
  72. struct descriptor descriptor;
  73. struct ar_buffer *next;
  74. __le32 data[0];
  75. };
  76. struct ar_context {
  77. struct fw_ohci *ohci;
  78. struct ar_buffer *current_buffer;
  79. struct ar_buffer *last_buffer;
  80. void *pointer;
  81. u32 regs;
  82. struct tasklet_struct tasklet;
  83. };
  84. struct context;
  85. typedef int (*descriptor_callback_t)(struct context *ctx,
  86. struct descriptor *d,
  87. struct descriptor *last);
  88. struct context {
  89. struct fw_ohci *ohci;
  90. u32 regs;
  91. struct descriptor *buffer;
  92. dma_addr_t buffer_bus;
  93. size_t buffer_size;
  94. struct descriptor *head_descriptor;
  95. struct descriptor *tail_descriptor;
  96. struct descriptor *tail_descriptor_last;
  97. struct descriptor *prev_descriptor;
  98. descriptor_callback_t callback;
  99. struct tasklet_struct tasklet;
  100. };
  101. #define IT_HEADER_SY(v) ((v) << 0)
  102. #define IT_HEADER_TCODE(v) ((v) << 4)
  103. #define IT_HEADER_CHANNEL(v) ((v) << 8)
  104. #define IT_HEADER_TAG(v) ((v) << 14)
  105. #define IT_HEADER_SPEED(v) ((v) << 16)
  106. #define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
  107. struct iso_context {
  108. struct fw_iso_context base;
  109. struct context context;
  110. void *header;
  111. size_t header_length;
  112. };
  113. #define CONFIG_ROM_SIZE 1024
  114. struct fw_ohci {
  115. struct fw_card card;
  116. u32 version;
  117. __iomem char *registers;
  118. dma_addr_t self_id_bus;
  119. __le32 *self_id_cpu;
  120. struct tasklet_struct bus_reset_tasklet;
  121. int node_id;
  122. int generation;
  123. int request_generation;
  124. u32 bus_seconds;
  125. /*
  126. * Spinlock for accessing fw_ohci data. Never call out of
  127. * this driver with this lock held.
  128. */
  129. spinlock_t lock;
  130. u32 self_id_buffer[512];
  131. /* Config rom buffers */
  132. __be32 *config_rom;
  133. dma_addr_t config_rom_bus;
  134. __be32 *next_config_rom;
  135. dma_addr_t next_config_rom_bus;
  136. u32 next_header;
  137. struct ar_context ar_request_ctx;
  138. struct ar_context ar_response_ctx;
  139. struct context at_request_ctx;
  140. struct context at_response_ctx;
  141. u32 it_context_mask;
  142. struct iso_context *it_context_list;
  143. u32 ir_context_mask;
  144. struct iso_context *ir_context_list;
  145. };
  146. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  147. {
  148. return container_of(card, struct fw_ohci, card);
  149. }
  150. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  151. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  152. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  153. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  154. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  155. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  156. #define CONTEXT_RUN 0x8000
  157. #define CONTEXT_WAKE 0x1000
  158. #define CONTEXT_DEAD 0x0800
  159. #define CONTEXT_ACTIVE 0x0400
  160. #define OHCI1394_MAX_AT_REQ_RETRIES 0x2
  161. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  162. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  163. #define FW_OHCI_MAJOR 240
  164. #define OHCI1394_REGISTER_SIZE 0x800
  165. #define OHCI_LOOP_COUNT 500
  166. #define OHCI1394_PCI_HCI_Control 0x40
  167. #define SELF_ID_BUF_SIZE 0x800
  168. #define OHCI_TCODE_PHY_PACKET 0x0e
  169. #define OHCI_VERSION_1_1 0x010010
  170. #define ISO_BUFFER_SIZE (64 * 1024)
  171. #define AT_BUFFER_SIZE 4096
  172. static char ohci_driver_name[] = KBUILD_MODNAME;
  173. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  174. {
  175. writel(data, ohci->registers + offset);
  176. }
  177. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  178. {
  179. return readl(ohci->registers + offset);
  180. }
  181. static inline void flush_writes(const struct fw_ohci *ohci)
  182. {
  183. /* Do a dummy read to flush writes. */
  184. reg_read(ohci, OHCI1394_Version);
  185. }
  186. static int
  187. ohci_update_phy_reg(struct fw_card *card, int addr,
  188. int clear_bits, int set_bits)
  189. {
  190. struct fw_ohci *ohci = fw_ohci(card);
  191. u32 val, old;
  192. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  193. msleep(2);
  194. val = reg_read(ohci, OHCI1394_PhyControl);
  195. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  196. fw_error("failed to set phy reg bits.\n");
  197. return -EBUSY;
  198. }
  199. old = OHCI1394_PhyControl_ReadData(val);
  200. old = (old & ~clear_bits) | set_bits;
  201. reg_write(ohci, OHCI1394_PhyControl,
  202. OHCI1394_PhyControl_Write(addr, old));
  203. return 0;
  204. }
  205. static int ar_context_add_page(struct ar_context *ctx)
  206. {
  207. struct device *dev = ctx->ohci->card.device;
  208. struct ar_buffer *ab;
  209. dma_addr_t ab_bus;
  210. size_t offset;
  211. ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
  212. if (ab == NULL)
  213. return -ENOMEM;
  214. ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
  215. if (dma_mapping_error(ab_bus)) {
  216. free_page((unsigned long) ab);
  217. return -ENOMEM;
  218. }
  219. memset(&ab->descriptor, 0, sizeof(ab->descriptor));
  220. ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  221. DESCRIPTOR_STATUS |
  222. DESCRIPTOR_BRANCH_ALWAYS);
  223. offset = offsetof(struct ar_buffer, data);
  224. ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
  225. ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
  226. ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
  227. ab->descriptor.branch_address = 0;
  228. dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  229. ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
  230. ctx->last_buffer->next = ab;
  231. ctx->last_buffer = ab;
  232. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  233. flush_writes(ctx->ohci);
  234. return 0;
  235. }
  236. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  237. {
  238. struct fw_ohci *ohci = ctx->ohci;
  239. struct fw_packet p;
  240. u32 status, length, tcode;
  241. p.header[0] = le32_to_cpu(buffer[0]);
  242. p.header[1] = le32_to_cpu(buffer[1]);
  243. p.header[2] = le32_to_cpu(buffer[2]);
  244. tcode = (p.header[0] >> 4) & 0x0f;
  245. switch (tcode) {
  246. case TCODE_WRITE_QUADLET_REQUEST:
  247. case TCODE_READ_QUADLET_RESPONSE:
  248. p.header[3] = (__force __u32) buffer[3];
  249. p.header_length = 16;
  250. p.payload_length = 0;
  251. break;
  252. case TCODE_READ_BLOCK_REQUEST :
  253. p.header[3] = le32_to_cpu(buffer[3]);
  254. p.header_length = 16;
  255. p.payload_length = 0;
  256. break;
  257. case TCODE_WRITE_BLOCK_REQUEST:
  258. case TCODE_READ_BLOCK_RESPONSE:
  259. case TCODE_LOCK_REQUEST:
  260. case TCODE_LOCK_RESPONSE:
  261. p.header[3] = le32_to_cpu(buffer[3]);
  262. p.header_length = 16;
  263. p.payload_length = p.header[3] >> 16;
  264. break;
  265. case TCODE_WRITE_RESPONSE:
  266. case TCODE_READ_QUADLET_REQUEST:
  267. case OHCI_TCODE_PHY_PACKET:
  268. p.header_length = 12;
  269. p.payload_length = 0;
  270. break;
  271. }
  272. p.payload = (void *) buffer + p.header_length;
  273. /* FIXME: What to do about evt_* errors? */
  274. length = (p.header_length + p.payload_length + 3) / 4;
  275. status = le32_to_cpu(buffer[length]);
  276. p.ack = ((status >> 16) & 0x1f) - 16;
  277. p.speed = (status >> 21) & 0x7;
  278. p.timestamp = status & 0xffff;
  279. p.generation = ohci->request_generation;
  280. /*
  281. * The OHCI bus reset handler synthesizes a phy packet with
  282. * the new generation number when a bus reset happens (see
  283. * section 8.4.2.3). This helps us determine when a request
  284. * was received and make sure we send the response in the same
  285. * generation. We only need this for requests; for responses
  286. * we use the unique tlabel for finding the matching
  287. * request.
  288. */
  289. if (p.ack + 16 == 0x09)
  290. ohci->request_generation = (buffer[2] >> 16) & 0xff;
  291. else if (ctx == &ohci->ar_request_ctx)
  292. fw_core_handle_request(&ohci->card, &p);
  293. else
  294. fw_core_handle_response(&ohci->card, &p);
  295. return buffer + length + 1;
  296. }
  297. static void ar_context_tasklet(unsigned long data)
  298. {
  299. struct ar_context *ctx = (struct ar_context *)data;
  300. struct fw_ohci *ohci = ctx->ohci;
  301. struct ar_buffer *ab;
  302. struct descriptor *d;
  303. void *buffer, *end;
  304. ab = ctx->current_buffer;
  305. d = &ab->descriptor;
  306. if (d->res_count == 0) {
  307. size_t size, rest, offset;
  308. /*
  309. * This descriptor is finished and we may have a
  310. * packet split across this and the next buffer. We
  311. * reuse the page for reassembling the split packet.
  312. */
  313. offset = offsetof(struct ar_buffer, data);
  314. dma_unmap_single(ohci->card.device,
  315. ab->descriptor.data_address - offset,
  316. PAGE_SIZE, DMA_BIDIRECTIONAL);
  317. buffer = ab;
  318. ab = ab->next;
  319. d = &ab->descriptor;
  320. size = buffer + PAGE_SIZE - ctx->pointer;
  321. rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
  322. memmove(buffer, ctx->pointer, size);
  323. memcpy(buffer + size, ab->data, rest);
  324. ctx->current_buffer = ab;
  325. ctx->pointer = (void *) ab->data + rest;
  326. end = buffer + size + rest;
  327. while (buffer < end)
  328. buffer = handle_ar_packet(ctx, buffer);
  329. free_page((unsigned long)buffer);
  330. ar_context_add_page(ctx);
  331. } else {
  332. buffer = ctx->pointer;
  333. ctx->pointer = end =
  334. (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
  335. while (buffer < end)
  336. buffer = handle_ar_packet(ctx, buffer);
  337. }
  338. }
  339. static int
  340. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
  341. {
  342. struct ar_buffer ab;
  343. ctx->regs = regs;
  344. ctx->ohci = ohci;
  345. ctx->last_buffer = &ab;
  346. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  347. ar_context_add_page(ctx);
  348. ar_context_add_page(ctx);
  349. ctx->current_buffer = ab.next;
  350. ctx->pointer = ctx->current_buffer->data;
  351. reg_write(ctx->ohci, COMMAND_PTR(ctx->regs),
  352. le32_to_cpu(ab.descriptor.branch_address));
  353. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
  354. flush_writes(ctx->ohci);
  355. return 0;
  356. }
  357. static void context_tasklet(unsigned long data)
  358. {
  359. struct context *ctx = (struct context *) data;
  360. struct fw_ohci *ohci = ctx->ohci;
  361. struct descriptor *d, *last;
  362. u32 address;
  363. int z;
  364. dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
  365. ctx->buffer_size, DMA_TO_DEVICE);
  366. d = ctx->tail_descriptor;
  367. last = ctx->tail_descriptor_last;
  368. while (last->branch_address != 0) {
  369. address = le32_to_cpu(last->branch_address);
  370. z = address & 0xf;
  371. d = ctx->buffer + (address - ctx->buffer_bus) / sizeof(*d);
  372. last = (z == 2) ? d : d + z - 1;
  373. if (!ctx->callback(ctx, d, last))
  374. break;
  375. ctx->tail_descriptor = d;
  376. ctx->tail_descriptor_last = last;
  377. }
  378. }
  379. static int
  380. context_init(struct context *ctx, struct fw_ohci *ohci,
  381. size_t buffer_size, u32 regs,
  382. descriptor_callback_t callback)
  383. {
  384. ctx->ohci = ohci;
  385. ctx->regs = regs;
  386. ctx->buffer_size = buffer_size;
  387. ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
  388. if (ctx->buffer == NULL)
  389. return -ENOMEM;
  390. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  391. ctx->callback = callback;
  392. ctx->buffer_bus =
  393. dma_map_single(ohci->card.device, ctx->buffer,
  394. buffer_size, DMA_TO_DEVICE);
  395. if (dma_mapping_error(ctx->buffer_bus)) {
  396. kfree(ctx->buffer);
  397. return -ENOMEM;
  398. }
  399. ctx->head_descriptor = ctx->buffer;
  400. ctx->prev_descriptor = ctx->buffer;
  401. ctx->tail_descriptor = ctx->buffer;
  402. ctx->tail_descriptor_last = ctx->buffer;
  403. /*
  404. * We put a dummy descriptor in the buffer that has a NULL
  405. * branch address and looks like it's been sent. That way we
  406. * have a descriptor to append DMA programs to. Also, the
  407. * ring buffer invariant is that it always has at least one
  408. * element so that head == tail means buffer full.
  409. */
  410. memset(ctx->head_descriptor, 0, sizeof(*ctx->head_descriptor));
  411. ctx->head_descriptor->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
  412. ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
  413. ctx->head_descriptor++;
  414. return 0;
  415. }
  416. static void
  417. context_release(struct context *ctx)
  418. {
  419. struct fw_card *card = &ctx->ohci->card;
  420. dma_unmap_single(card->device, ctx->buffer_bus,
  421. ctx->buffer_size, DMA_TO_DEVICE);
  422. kfree(ctx->buffer);
  423. }
  424. static struct descriptor *
  425. context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
  426. {
  427. struct descriptor *d, *tail, *end;
  428. d = ctx->head_descriptor;
  429. tail = ctx->tail_descriptor;
  430. end = ctx->buffer + ctx->buffer_size / sizeof(*d);
  431. if (d + z <= tail) {
  432. goto has_space;
  433. } else if (d > tail && d + z <= end) {
  434. goto has_space;
  435. } else if (d > tail && ctx->buffer + z <= tail) {
  436. d = ctx->buffer;
  437. goto has_space;
  438. }
  439. return NULL;
  440. has_space:
  441. memset(d, 0, z * sizeof(*d));
  442. *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
  443. return d;
  444. }
  445. static void context_run(struct context *ctx, u32 extra)
  446. {
  447. struct fw_ohci *ohci = ctx->ohci;
  448. reg_write(ohci, COMMAND_PTR(ctx->regs),
  449. le32_to_cpu(ctx->tail_descriptor_last->branch_address));
  450. reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
  451. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
  452. flush_writes(ohci);
  453. }
  454. static void context_append(struct context *ctx,
  455. struct descriptor *d, int z, int extra)
  456. {
  457. dma_addr_t d_bus;
  458. d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
  459. ctx->head_descriptor = d + z + extra;
  460. ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
  461. ctx->prev_descriptor = z == 2 ? d : d + z - 1;
  462. dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
  463. ctx->buffer_size, DMA_TO_DEVICE);
  464. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  465. flush_writes(ctx->ohci);
  466. }
  467. static void context_stop(struct context *ctx)
  468. {
  469. u32 reg;
  470. int i;
  471. reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  472. flush_writes(ctx->ohci);
  473. for (i = 0; i < 10; i++) {
  474. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  475. if ((reg & CONTEXT_ACTIVE) == 0)
  476. break;
  477. fw_notify("context_stop: still active (0x%08x)\n", reg);
  478. msleep(1);
  479. }
  480. }
  481. struct driver_data {
  482. struct fw_packet *packet;
  483. };
  484. /*
  485. * This function apppends a packet to the DMA queue for transmission.
  486. * Must always be called with the ochi->lock held to ensure proper
  487. * generation handling and locking around packet queue manipulation.
  488. */
  489. static int
  490. at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
  491. {
  492. struct fw_ohci *ohci = ctx->ohci;
  493. dma_addr_t d_bus, payload_bus;
  494. struct driver_data *driver_data;
  495. struct descriptor *d, *last;
  496. __le32 *header;
  497. int z, tcode;
  498. u32 reg;
  499. d = context_get_descriptors(ctx, 4, &d_bus);
  500. if (d == NULL) {
  501. packet->ack = RCODE_SEND_ERROR;
  502. return -1;
  503. }
  504. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  505. d[0].res_count = cpu_to_le16(packet->timestamp);
  506. /*
  507. * The DMA format for asyncronous link packets is different
  508. * from the IEEE1394 layout, so shift the fields around
  509. * accordingly. If header_length is 8, it's a PHY packet, to
  510. * which we need to prepend an extra quadlet.
  511. */
  512. header = (__le32 *) &d[1];
  513. if (packet->header_length > 8) {
  514. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  515. (packet->speed << 16));
  516. header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  517. (packet->header[0] & 0xffff0000));
  518. header[2] = cpu_to_le32(packet->header[2]);
  519. tcode = (packet->header[0] >> 4) & 0x0f;
  520. if (TCODE_IS_BLOCK_PACKET(tcode))
  521. header[3] = cpu_to_le32(packet->header[3]);
  522. else
  523. header[3] = (__force __le32) packet->header[3];
  524. d[0].req_count = cpu_to_le16(packet->header_length);
  525. } else {
  526. header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
  527. (packet->speed << 16));
  528. header[1] = cpu_to_le32(packet->header[0]);
  529. header[2] = cpu_to_le32(packet->header[1]);
  530. d[0].req_count = cpu_to_le16(12);
  531. }
  532. driver_data = (struct driver_data *) &d[3];
  533. driver_data->packet = packet;
  534. packet->driver_data = driver_data;
  535. if (packet->payload_length > 0) {
  536. payload_bus =
  537. dma_map_single(ohci->card.device, packet->payload,
  538. packet->payload_length, DMA_TO_DEVICE);
  539. if (dma_mapping_error(payload_bus)) {
  540. packet->ack = RCODE_SEND_ERROR;
  541. return -1;
  542. }
  543. d[2].req_count = cpu_to_le16(packet->payload_length);
  544. d[2].data_address = cpu_to_le32(payload_bus);
  545. last = &d[2];
  546. z = 3;
  547. } else {
  548. last = &d[0];
  549. z = 2;
  550. }
  551. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  552. DESCRIPTOR_IRQ_ALWAYS |
  553. DESCRIPTOR_BRANCH_ALWAYS);
  554. /* FIXME: Document how the locking works. */
  555. if (ohci->generation != packet->generation) {
  556. packet->ack = RCODE_GENERATION;
  557. return -1;
  558. }
  559. context_append(ctx, d, z, 4 - z);
  560. /* If the context isn't already running, start it up. */
  561. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  562. if ((reg & CONTEXT_RUN) == 0)
  563. context_run(ctx, 0);
  564. return 0;
  565. }
  566. static int handle_at_packet(struct context *context,
  567. struct descriptor *d,
  568. struct descriptor *last)
  569. {
  570. struct driver_data *driver_data;
  571. struct fw_packet *packet;
  572. struct fw_ohci *ohci = context->ohci;
  573. dma_addr_t payload_bus;
  574. int evt;
  575. if (last->transfer_status == 0)
  576. /* This descriptor isn't done yet, stop iteration. */
  577. return 0;
  578. driver_data = (struct driver_data *) &d[3];
  579. packet = driver_data->packet;
  580. if (packet == NULL)
  581. /* This packet was cancelled, just continue. */
  582. return 1;
  583. payload_bus = le32_to_cpu(last->data_address);
  584. if (payload_bus != 0)
  585. dma_unmap_single(ohci->card.device, payload_bus,
  586. packet->payload_length, DMA_TO_DEVICE);
  587. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  588. packet->timestamp = le16_to_cpu(last->res_count);
  589. switch (evt) {
  590. case OHCI1394_evt_timeout:
  591. /* Async response transmit timed out. */
  592. packet->ack = RCODE_CANCELLED;
  593. break;
  594. case OHCI1394_evt_flushed:
  595. /*
  596. * The packet was flushed should give same error as
  597. * when we try to use a stale generation count.
  598. */
  599. packet->ack = RCODE_GENERATION;
  600. break;
  601. case OHCI1394_evt_missing_ack:
  602. /*
  603. * Using a valid (current) generation count, but the
  604. * node is not on the bus or not sending acks.
  605. */
  606. packet->ack = RCODE_NO_ACK;
  607. break;
  608. case ACK_COMPLETE + 0x10:
  609. case ACK_PENDING + 0x10:
  610. case ACK_BUSY_X + 0x10:
  611. case ACK_BUSY_A + 0x10:
  612. case ACK_BUSY_B + 0x10:
  613. case ACK_DATA_ERROR + 0x10:
  614. case ACK_TYPE_ERROR + 0x10:
  615. packet->ack = evt - 0x10;
  616. break;
  617. default:
  618. packet->ack = RCODE_SEND_ERROR;
  619. break;
  620. }
  621. packet->callback(packet, &ohci->card, packet->ack);
  622. return 1;
  623. }
  624. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  625. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  626. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  627. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  628. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  629. static void
  630. handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  631. {
  632. struct fw_packet response;
  633. int tcode, length, i;
  634. tcode = HEADER_GET_TCODE(packet->header[0]);
  635. if (TCODE_IS_BLOCK_PACKET(tcode))
  636. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  637. else
  638. length = 4;
  639. i = csr - CSR_CONFIG_ROM;
  640. if (i + length > CONFIG_ROM_SIZE) {
  641. fw_fill_response(&response, packet->header,
  642. RCODE_ADDRESS_ERROR, NULL, 0);
  643. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  644. fw_fill_response(&response, packet->header,
  645. RCODE_TYPE_ERROR, NULL, 0);
  646. } else {
  647. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  648. (void *) ohci->config_rom + i, length);
  649. }
  650. fw_core_handle_response(&ohci->card, &response);
  651. }
  652. static void
  653. handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  654. {
  655. struct fw_packet response;
  656. int tcode, length, ext_tcode, sel;
  657. __be32 *payload, lock_old;
  658. u32 lock_arg, lock_data;
  659. tcode = HEADER_GET_TCODE(packet->header[0]);
  660. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  661. payload = packet->payload;
  662. ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
  663. if (tcode == TCODE_LOCK_REQUEST &&
  664. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  665. lock_arg = be32_to_cpu(payload[0]);
  666. lock_data = be32_to_cpu(payload[1]);
  667. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  668. lock_arg = 0;
  669. lock_data = 0;
  670. } else {
  671. fw_fill_response(&response, packet->header,
  672. RCODE_TYPE_ERROR, NULL, 0);
  673. goto out;
  674. }
  675. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  676. reg_write(ohci, OHCI1394_CSRData, lock_data);
  677. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  678. reg_write(ohci, OHCI1394_CSRControl, sel);
  679. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  680. lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
  681. else
  682. fw_notify("swap not done yet\n");
  683. fw_fill_response(&response, packet->header,
  684. RCODE_COMPLETE, &lock_old, sizeof(lock_old));
  685. out:
  686. fw_core_handle_response(&ohci->card, &response);
  687. }
  688. static void
  689. handle_local_request(struct context *ctx, struct fw_packet *packet)
  690. {
  691. u64 offset;
  692. u32 csr;
  693. if (ctx == &ctx->ohci->at_request_ctx) {
  694. packet->ack = ACK_PENDING;
  695. packet->callback(packet, &ctx->ohci->card, packet->ack);
  696. }
  697. offset =
  698. ((unsigned long long)
  699. HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
  700. packet->header[2];
  701. csr = offset - CSR_REGISTER_BASE;
  702. /* Handle config rom reads. */
  703. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  704. handle_local_rom(ctx->ohci, packet, csr);
  705. else switch (csr) {
  706. case CSR_BUS_MANAGER_ID:
  707. case CSR_BANDWIDTH_AVAILABLE:
  708. case CSR_CHANNELS_AVAILABLE_HI:
  709. case CSR_CHANNELS_AVAILABLE_LO:
  710. handle_local_lock(ctx->ohci, packet, csr);
  711. break;
  712. default:
  713. if (ctx == &ctx->ohci->at_request_ctx)
  714. fw_core_handle_request(&ctx->ohci->card, packet);
  715. else
  716. fw_core_handle_response(&ctx->ohci->card, packet);
  717. break;
  718. }
  719. if (ctx == &ctx->ohci->at_response_ctx) {
  720. packet->ack = ACK_COMPLETE;
  721. packet->callback(packet, &ctx->ohci->card, packet->ack);
  722. }
  723. }
  724. static void
  725. at_context_transmit(struct context *ctx, struct fw_packet *packet)
  726. {
  727. unsigned long flags;
  728. int retval;
  729. spin_lock_irqsave(&ctx->ohci->lock, flags);
  730. if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
  731. ctx->ohci->generation == packet->generation) {
  732. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  733. handle_local_request(ctx, packet);
  734. return;
  735. }
  736. retval = at_context_queue_packet(ctx, packet);
  737. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  738. if (retval < 0)
  739. packet->callback(packet, &ctx->ohci->card, packet->ack);
  740. }
  741. static void bus_reset_tasklet(unsigned long data)
  742. {
  743. struct fw_ohci *ohci = (struct fw_ohci *)data;
  744. int self_id_count, i, j, reg;
  745. int generation, new_generation;
  746. unsigned long flags;
  747. reg = reg_read(ohci, OHCI1394_NodeID);
  748. if (!(reg & OHCI1394_NodeID_idValid)) {
  749. fw_error("node ID not valid, new bus reset in progress\n");
  750. return;
  751. }
  752. ohci->node_id = reg & 0xffff;
  753. /*
  754. * The count in the SelfIDCount register is the number of
  755. * bytes in the self ID receive buffer. Since we also receive
  756. * the inverted quadlets and a header quadlet, we shift one
  757. * bit extra to get the actual number of self IDs.
  758. */
  759. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  760. generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  761. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  762. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  763. fw_error("inconsistent self IDs\n");
  764. ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
  765. }
  766. /*
  767. * Check the consistency of the self IDs we just read. The
  768. * problem we face is that a new bus reset can start while we
  769. * read out the self IDs from the DMA buffer. If this happens,
  770. * the DMA buffer will be overwritten with new self IDs and we
  771. * will read out inconsistent data. The OHCI specification
  772. * (section 11.2) recommends a technique similar to
  773. * linux/seqlock.h, where we remember the generation of the
  774. * self IDs in the buffer before reading them out and compare
  775. * it to the current generation after reading them out. If
  776. * the two generations match we know we have a consistent set
  777. * of self IDs.
  778. */
  779. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  780. if (new_generation != generation) {
  781. fw_notify("recursive bus reset detected, "
  782. "discarding self ids\n");
  783. return;
  784. }
  785. /* FIXME: Document how the locking works. */
  786. spin_lock_irqsave(&ohci->lock, flags);
  787. ohci->generation = generation;
  788. context_stop(&ohci->at_request_ctx);
  789. context_stop(&ohci->at_response_ctx);
  790. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  791. /*
  792. * This next bit is unrelated to the AT context stuff but we
  793. * have to do it under the spinlock also. If a new config rom
  794. * was set up before this reset, the old one is now no longer
  795. * in use and we can free it. Update the config rom pointers
  796. * to point to the current config rom and clear the
  797. * next_config_rom pointer so a new udpate can take place.
  798. */
  799. if (ohci->next_config_rom != NULL) {
  800. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  801. ohci->config_rom, ohci->config_rom_bus);
  802. ohci->config_rom = ohci->next_config_rom;
  803. ohci->config_rom_bus = ohci->next_config_rom_bus;
  804. ohci->next_config_rom = NULL;
  805. /*
  806. * Restore config_rom image and manually update
  807. * config_rom registers. Writing the header quadlet
  808. * will indicate that the config rom is ready, so we
  809. * do that last.
  810. */
  811. reg_write(ohci, OHCI1394_BusOptions,
  812. be32_to_cpu(ohci->config_rom[2]));
  813. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  814. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  815. }
  816. spin_unlock_irqrestore(&ohci->lock, flags);
  817. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  818. self_id_count, ohci->self_id_buffer);
  819. }
  820. static irqreturn_t irq_handler(int irq, void *data)
  821. {
  822. struct fw_ohci *ohci = data;
  823. u32 event, iso_event, cycle_time;
  824. int i;
  825. event = reg_read(ohci, OHCI1394_IntEventClear);
  826. if (!event)
  827. return IRQ_NONE;
  828. reg_write(ohci, OHCI1394_IntEventClear, event);
  829. if (event & OHCI1394_selfIDComplete)
  830. tasklet_schedule(&ohci->bus_reset_tasklet);
  831. if (event & OHCI1394_RQPkt)
  832. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  833. if (event & OHCI1394_RSPkt)
  834. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  835. if (event & OHCI1394_reqTxComplete)
  836. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  837. if (event & OHCI1394_respTxComplete)
  838. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  839. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  840. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  841. while (iso_event) {
  842. i = ffs(iso_event) - 1;
  843. tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
  844. iso_event &= ~(1 << i);
  845. }
  846. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  847. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  848. while (iso_event) {
  849. i = ffs(iso_event) - 1;
  850. tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
  851. iso_event &= ~(1 << i);
  852. }
  853. if (event & OHCI1394_cycle64Seconds) {
  854. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  855. if ((cycle_time & 0x80000000) == 0)
  856. ohci->bus_seconds++;
  857. }
  858. return IRQ_HANDLED;
  859. }
  860. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  861. {
  862. struct fw_ohci *ohci = fw_ohci(card);
  863. struct pci_dev *dev = to_pci_dev(card->device);
  864. /*
  865. * When the link is not yet enabled, the atomic config rom
  866. * update mechanism described below in ohci_set_config_rom()
  867. * is not active. We have to update ConfigRomHeader and
  868. * BusOptions manually, and the write to ConfigROMmap takes
  869. * effect immediately. We tie this to the enabling of the
  870. * link, so we have a valid config rom before enabling - the
  871. * OHCI requires that ConfigROMhdr and BusOptions have valid
  872. * values before enabling.
  873. *
  874. * However, when the ConfigROMmap is written, some controllers
  875. * always read back quadlets 0 and 2 from the config rom to
  876. * the ConfigRomHeader and BusOptions registers on bus reset.
  877. * They shouldn't do that in this initial case where the link
  878. * isn't enabled. This means we have to use the same
  879. * workaround here, setting the bus header to 0 and then write
  880. * the right values in the bus reset tasklet.
  881. */
  882. ohci->next_config_rom =
  883. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  884. &ohci->next_config_rom_bus, GFP_KERNEL);
  885. if (ohci->next_config_rom == NULL)
  886. return -ENOMEM;
  887. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  888. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  889. ohci->next_header = config_rom[0];
  890. ohci->next_config_rom[0] = 0;
  891. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  892. reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
  893. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  894. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  895. if (request_irq(dev->irq, irq_handler,
  896. IRQF_SHARED, ohci_driver_name, ohci)) {
  897. fw_error("Failed to allocate shared interrupt %d.\n",
  898. dev->irq);
  899. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  900. ohci->config_rom, ohci->config_rom_bus);
  901. return -EIO;
  902. }
  903. reg_write(ohci, OHCI1394_HCControlSet,
  904. OHCI1394_HCControl_linkEnable |
  905. OHCI1394_HCControl_BIBimageValid);
  906. flush_writes(ohci);
  907. /*
  908. * We are ready to go, initiate bus reset to finish the
  909. * initialization.
  910. */
  911. fw_core_initiate_bus_reset(&ohci->card, 1);
  912. return 0;
  913. }
  914. static int
  915. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  916. {
  917. struct fw_ohci *ohci;
  918. unsigned long flags;
  919. int retval = 0;
  920. __be32 *next_config_rom;
  921. dma_addr_t next_config_rom_bus;
  922. ohci = fw_ohci(card);
  923. /*
  924. * When the OHCI controller is enabled, the config rom update
  925. * mechanism is a bit tricky, but easy enough to use. See
  926. * section 5.5.6 in the OHCI specification.
  927. *
  928. * The OHCI controller caches the new config rom address in a
  929. * shadow register (ConfigROMmapNext) and needs a bus reset
  930. * for the changes to take place. When the bus reset is
  931. * detected, the controller loads the new values for the
  932. * ConfigRomHeader and BusOptions registers from the specified
  933. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  934. * shadow register. All automatically and atomically.
  935. *
  936. * Now, there's a twist to this story. The automatic load of
  937. * ConfigRomHeader and BusOptions doesn't honor the
  938. * noByteSwapData bit, so with a be32 config rom, the
  939. * controller will load be32 values in to these registers
  940. * during the atomic update, even on litte endian
  941. * architectures. The workaround we use is to put a 0 in the
  942. * header quadlet; 0 is endian agnostic and means that the
  943. * config rom isn't ready yet. In the bus reset tasklet we
  944. * then set up the real values for the two registers.
  945. *
  946. * We use ohci->lock to avoid racing with the code that sets
  947. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  948. */
  949. next_config_rom =
  950. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  951. &next_config_rom_bus, GFP_KERNEL);
  952. if (next_config_rom == NULL)
  953. return -ENOMEM;
  954. spin_lock_irqsave(&ohci->lock, flags);
  955. if (ohci->next_config_rom == NULL) {
  956. ohci->next_config_rom = next_config_rom;
  957. ohci->next_config_rom_bus = next_config_rom_bus;
  958. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  959. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  960. length * 4);
  961. ohci->next_header = config_rom[0];
  962. ohci->next_config_rom[0] = 0;
  963. reg_write(ohci, OHCI1394_ConfigROMmap,
  964. ohci->next_config_rom_bus);
  965. } else {
  966. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  967. next_config_rom, next_config_rom_bus);
  968. retval = -EBUSY;
  969. }
  970. spin_unlock_irqrestore(&ohci->lock, flags);
  971. /*
  972. * Now initiate a bus reset to have the changes take
  973. * effect. We clean up the old config rom memory and DMA
  974. * mappings in the bus reset tasklet, since the OHCI
  975. * controller could need to access it before the bus reset
  976. * takes effect.
  977. */
  978. if (retval == 0)
  979. fw_core_initiate_bus_reset(&ohci->card, 1);
  980. return retval;
  981. }
  982. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  983. {
  984. struct fw_ohci *ohci = fw_ohci(card);
  985. at_context_transmit(&ohci->at_request_ctx, packet);
  986. }
  987. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  988. {
  989. struct fw_ohci *ohci = fw_ohci(card);
  990. at_context_transmit(&ohci->at_response_ctx, packet);
  991. }
  992. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  993. {
  994. struct fw_ohci *ohci = fw_ohci(card);
  995. struct context *ctx = &ohci->at_request_ctx;
  996. struct driver_data *driver_data = packet->driver_data;
  997. int retval = -ENOENT;
  998. tasklet_disable(&ctx->tasklet);
  999. if (packet->ack != 0)
  1000. goto out;
  1001. driver_data->packet = NULL;
  1002. packet->ack = RCODE_CANCELLED;
  1003. packet->callback(packet, &ohci->card, packet->ack);
  1004. retval = 0;
  1005. out:
  1006. tasklet_enable(&ctx->tasklet);
  1007. return retval;
  1008. }
  1009. static int
  1010. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  1011. {
  1012. struct fw_ohci *ohci = fw_ohci(card);
  1013. unsigned long flags;
  1014. int n, retval = 0;
  1015. /*
  1016. * FIXME: Make sure this bitmask is cleared when we clear the busReset
  1017. * interrupt bit. Clear physReqResourceAllBuses on bus reset.
  1018. */
  1019. spin_lock_irqsave(&ohci->lock, flags);
  1020. if (ohci->generation != generation) {
  1021. retval = -ESTALE;
  1022. goto out;
  1023. }
  1024. /*
  1025. * Note, if the node ID contains a non-local bus ID, physical DMA is
  1026. * enabled for _all_ nodes on remote buses.
  1027. */
  1028. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  1029. if (n < 32)
  1030. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  1031. else
  1032. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  1033. flush_writes(ohci);
  1034. out:
  1035. spin_unlock_irqrestore(&ohci->lock, flags);
  1036. return retval;
  1037. }
  1038. static u64
  1039. ohci_get_bus_time(struct fw_card *card)
  1040. {
  1041. struct fw_ohci *ohci = fw_ohci(card);
  1042. u32 cycle_time;
  1043. u64 bus_time;
  1044. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1045. bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
  1046. return bus_time;
  1047. }
  1048. static int handle_ir_dualbuffer_packet(struct context *context,
  1049. struct descriptor *d,
  1050. struct descriptor *last)
  1051. {
  1052. struct iso_context *ctx =
  1053. container_of(context, struct iso_context, context);
  1054. struct db_descriptor *db = (struct db_descriptor *) d;
  1055. __le32 *ir_header;
  1056. size_t header_length;
  1057. void *p, *end;
  1058. int i;
  1059. if (db->first_res_count > 0 && db->second_res_count > 0)
  1060. /* This descriptor isn't done yet, stop iteration. */
  1061. return 0;
  1062. header_length = le16_to_cpu(db->first_req_count) -
  1063. le16_to_cpu(db->first_res_count);
  1064. i = ctx->header_length;
  1065. p = db + 1;
  1066. end = p + header_length;
  1067. while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
  1068. /*
  1069. * The iso header is byteswapped to little endian by
  1070. * the controller, but the remaining header quadlets
  1071. * are big endian. We want to present all the headers
  1072. * as big endian, so we have to swap the first
  1073. * quadlet.
  1074. */
  1075. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  1076. memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
  1077. i += ctx->base.header_size;
  1078. p += ctx->base.header_size + 4;
  1079. }
  1080. ctx->header_length = i;
  1081. if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1082. ir_header = (__le32 *) (db + 1);
  1083. ctx->base.callback(&ctx->base,
  1084. le32_to_cpu(ir_header[0]) & 0xffff,
  1085. ctx->header_length, ctx->header,
  1086. ctx->base.callback_data);
  1087. ctx->header_length = 0;
  1088. }
  1089. return 1;
  1090. }
  1091. static int handle_it_packet(struct context *context,
  1092. struct descriptor *d,
  1093. struct descriptor *last)
  1094. {
  1095. struct iso_context *ctx =
  1096. container_of(context, struct iso_context, context);
  1097. if (last->transfer_status == 0)
  1098. /* This descriptor isn't done yet, stop iteration. */
  1099. return 0;
  1100. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
  1101. ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
  1102. 0, NULL, ctx->base.callback_data);
  1103. return 1;
  1104. }
  1105. static struct fw_iso_context *
  1106. ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
  1107. {
  1108. struct fw_ohci *ohci = fw_ohci(card);
  1109. struct iso_context *ctx, *list;
  1110. descriptor_callback_t callback;
  1111. u32 *mask, regs;
  1112. unsigned long flags;
  1113. int index, retval = -ENOMEM;
  1114. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  1115. mask = &ohci->it_context_mask;
  1116. list = ohci->it_context_list;
  1117. callback = handle_it_packet;
  1118. } else {
  1119. mask = &ohci->ir_context_mask;
  1120. list = ohci->ir_context_list;
  1121. callback = handle_ir_dualbuffer_packet;
  1122. }
  1123. /* FIXME: We need a fallback for pre 1.1 OHCI. */
  1124. if (callback == handle_ir_dualbuffer_packet &&
  1125. ohci->version < OHCI_VERSION_1_1)
  1126. return ERR_PTR(-EINVAL);
  1127. spin_lock_irqsave(&ohci->lock, flags);
  1128. index = ffs(*mask) - 1;
  1129. if (index >= 0)
  1130. *mask &= ~(1 << index);
  1131. spin_unlock_irqrestore(&ohci->lock, flags);
  1132. if (index < 0)
  1133. return ERR_PTR(-EBUSY);
  1134. if (type == FW_ISO_CONTEXT_TRANSMIT)
  1135. regs = OHCI1394_IsoXmitContextBase(index);
  1136. else
  1137. regs = OHCI1394_IsoRcvContextBase(index);
  1138. ctx = &list[index];
  1139. memset(ctx, 0, sizeof(*ctx));
  1140. ctx->header_length = 0;
  1141. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  1142. if (ctx->header == NULL)
  1143. goto out;
  1144. retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
  1145. regs, callback);
  1146. if (retval < 0)
  1147. goto out_with_header;
  1148. return &ctx->base;
  1149. out_with_header:
  1150. free_page((unsigned long)ctx->header);
  1151. out:
  1152. spin_lock_irqsave(&ohci->lock, flags);
  1153. *mask |= 1 << index;
  1154. spin_unlock_irqrestore(&ohci->lock, flags);
  1155. return ERR_PTR(retval);
  1156. }
  1157. static int ohci_start_iso(struct fw_iso_context *base,
  1158. s32 cycle, u32 sync, u32 tags)
  1159. {
  1160. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1161. struct fw_ohci *ohci = ctx->context.ohci;
  1162. u32 control, match;
  1163. int index;
  1164. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1165. index = ctx - ohci->it_context_list;
  1166. match = 0;
  1167. if (cycle >= 0)
  1168. match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  1169. (cycle & 0x7fff) << 16;
  1170. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  1171. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  1172. context_run(&ctx->context, match);
  1173. } else {
  1174. index = ctx - ohci->ir_context_list;
  1175. control = IR_CONTEXT_DUAL_BUFFER_MODE | IR_CONTEXT_ISOCH_HEADER;
  1176. match = (tags << 28) | (sync << 8) | ctx->base.channel;
  1177. if (cycle >= 0) {
  1178. match |= (cycle & 0x07fff) << 12;
  1179. control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
  1180. }
  1181. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  1182. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  1183. reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
  1184. context_run(&ctx->context, control);
  1185. }
  1186. return 0;
  1187. }
  1188. static int ohci_stop_iso(struct fw_iso_context *base)
  1189. {
  1190. struct fw_ohci *ohci = fw_ohci(base->card);
  1191. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1192. int index;
  1193. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1194. index = ctx - ohci->it_context_list;
  1195. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1196. } else {
  1197. index = ctx - ohci->ir_context_list;
  1198. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1199. }
  1200. flush_writes(ohci);
  1201. context_stop(&ctx->context);
  1202. return 0;
  1203. }
  1204. static void ohci_free_iso_context(struct fw_iso_context *base)
  1205. {
  1206. struct fw_ohci *ohci = fw_ohci(base->card);
  1207. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1208. unsigned long flags;
  1209. int index;
  1210. ohci_stop_iso(base);
  1211. context_release(&ctx->context);
  1212. free_page((unsigned long)ctx->header);
  1213. spin_lock_irqsave(&ohci->lock, flags);
  1214. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1215. index = ctx - ohci->it_context_list;
  1216. ohci->it_context_mask |= 1 << index;
  1217. } else {
  1218. index = ctx - ohci->ir_context_list;
  1219. ohci->ir_context_mask |= 1 << index;
  1220. }
  1221. spin_unlock_irqrestore(&ohci->lock, flags);
  1222. }
  1223. static int
  1224. ohci_queue_iso_transmit(struct fw_iso_context *base,
  1225. struct fw_iso_packet *packet,
  1226. struct fw_iso_buffer *buffer,
  1227. unsigned long payload)
  1228. {
  1229. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1230. struct descriptor *d, *last, *pd;
  1231. struct fw_iso_packet *p;
  1232. __le32 *header;
  1233. dma_addr_t d_bus, page_bus;
  1234. u32 z, header_z, payload_z, irq;
  1235. u32 payload_index, payload_end_index, next_page_index;
  1236. int page, end_page, i, length, offset;
  1237. /*
  1238. * FIXME: Cycle lost behavior should be configurable: lose
  1239. * packet, retransmit or terminate..
  1240. */
  1241. p = packet;
  1242. payload_index = payload;
  1243. if (p->skip)
  1244. z = 1;
  1245. else
  1246. z = 2;
  1247. if (p->header_length > 0)
  1248. z++;
  1249. /* Determine the first page the payload isn't contained in. */
  1250. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1251. if (p->payload_length > 0)
  1252. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1253. else
  1254. payload_z = 0;
  1255. z += payload_z;
  1256. /* Get header size in number of descriptors. */
  1257. header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
  1258. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  1259. if (d == NULL)
  1260. return -ENOMEM;
  1261. if (!p->skip) {
  1262. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  1263. d[0].req_count = cpu_to_le16(8);
  1264. header = (__le32 *) &d[1];
  1265. header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
  1266. IT_HEADER_TAG(p->tag) |
  1267. IT_HEADER_TCODE(TCODE_STREAM_DATA) |
  1268. IT_HEADER_CHANNEL(ctx->base.channel) |
  1269. IT_HEADER_SPEED(ctx->base.speed));
  1270. header[1] =
  1271. cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
  1272. p->payload_length));
  1273. }
  1274. if (p->header_length > 0) {
  1275. d[2].req_count = cpu_to_le16(p->header_length);
  1276. d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
  1277. memcpy(&d[z], p->header, p->header_length);
  1278. }
  1279. pd = d + z - payload_z;
  1280. payload_end_index = payload_index + p->payload_length;
  1281. for (i = 0; i < payload_z; i++) {
  1282. page = payload_index >> PAGE_SHIFT;
  1283. offset = payload_index & ~PAGE_MASK;
  1284. next_page_index = (page + 1) << PAGE_SHIFT;
  1285. length =
  1286. min(next_page_index, payload_end_index) - payload_index;
  1287. pd[i].req_count = cpu_to_le16(length);
  1288. page_bus = page_private(buffer->pages[page]);
  1289. pd[i].data_address = cpu_to_le32(page_bus + offset);
  1290. payload_index += length;
  1291. }
  1292. if (p->interrupt)
  1293. irq = DESCRIPTOR_IRQ_ALWAYS;
  1294. else
  1295. irq = DESCRIPTOR_NO_IRQ;
  1296. last = z == 2 ? d : d + z - 1;
  1297. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  1298. DESCRIPTOR_STATUS |
  1299. DESCRIPTOR_BRANCH_ALWAYS |
  1300. irq);
  1301. context_append(&ctx->context, d, z, header_z);
  1302. return 0;
  1303. }
  1304. static int
  1305. ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
  1306. struct fw_iso_packet *packet,
  1307. struct fw_iso_buffer *buffer,
  1308. unsigned long payload)
  1309. {
  1310. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1311. struct db_descriptor *db = NULL;
  1312. struct descriptor *d;
  1313. struct fw_iso_packet *p;
  1314. dma_addr_t d_bus, page_bus;
  1315. u32 z, header_z, length, rest;
  1316. int page, offset, packet_count, header_size;
  1317. /*
  1318. * FIXME: Cycle lost behavior should be configurable: lose
  1319. * packet, retransmit or terminate..
  1320. */
  1321. if (packet->skip) {
  1322. d = context_get_descriptors(&ctx->context, 2, &d_bus);
  1323. if (d == NULL)
  1324. return -ENOMEM;
  1325. db = (struct db_descriptor *) d;
  1326. db->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1327. DESCRIPTOR_BRANCH_ALWAYS |
  1328. DESCRIPTOR_WAIT);
  1329. db->first_size = cpu_to_le16(ctx->base.header_size + 4);
  1330. context_append(&ctx->context, d, 2, 0);
  1331. }
  1332. p = packet;
  1333. z = 2;
  1334. /*
  1335. * The OHCI controller puts the status word in the header
  1336. * buffer too, so we need 4 extra bytes per packet.
  1337. */
  1338. packet_count = p->header_length / ctx->base.header_size;
  1339. header_size = packet_count * (ctx->base.header_size + 4);
  1340. /* Get header size in number of descriptors. */
  1341. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1342. page = payload >> PAGE_SHIFT;
  1343. offset = payload & ~PAGE_MASK;
  1344. rest = p->payload_length;
  1345. /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
  1346. /* FIXME: make packet-per-buffer/dual-buffer a context option */
  1347. while (rest > 0) {
  1348. d = context_get_descriptors(&ctx->context,
  1349. z + header_z, &d_bus);
  1350. if (d == NULL)
  1351. return -ENOMEM;
  1352. db = (struct db_descriptor *) d;
  1353. db->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1354. DESCRIPTOR_BRANCH_ALWAYS);
  1355. db->first_size = cpu_to_le16(ctx->base.header_size + 4);
  1356. db->first_req_count = cpu_to_le16(header_size);
  1357. db->first_res_count = db->first_req_count;
  1358. db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
  1359. if (offset + rest < PAGE_SIZE)
  1360. length = rest;
  1361. else
  1362. length = PAGE_SIZE - offset;
  1363. db->second_req_count = cpu_to_le16(length);
  1364. db->second_res_count = db->second_req_count;
  1365. page_bus = page_private(buffer->pages[page]);
  1366. db->second_buffer = cpu_to_le32(page_bus + offset);
  1367. if (p->interrupt && length == rest)
  1368. db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1369. context_append(&ctx->context, d, z, header_z);
  1370. offset = (offset + length) & ~PAGE_MASK;
  1371. rest -= length;
  1372. page++;
  1373. }
  1374. return 0;
  1375. }
  1376. static int
  1377. ohci_queue_iso(struct fw_iso_context *base,
  1378. struct fw_iso_packet *packet,
  1379. struct fw_iso_buffer *buffer,
  1380. unsigned long payload)
  1381. {
  1382. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1383. if (base->type == FW_ISO_CONTEXT_TRANSMIT)
  1384. return ohci_queue_iso_transmit(base, packet, buffer, payload);
  1385. else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
  1386. return ohci_queue_iso_receive_dualbuffer(base, packet,
  1387. buffer, payload);
  1388. else
  1389. /* FIXME: Implement fallback for OHCI 1.0 controllers. */
  1390. return -EINVAL;
  1391. }
  1392. static const struct fw_card_driver ohci_driver = {
  1393. .name = ohci_driver_name,
  1394. .enable = ohci_enable,
  1395. .update_phy_reg = ohci_update_phy_reg,
  1396. .set_config_rom = ohci_set_config_rom,
  1397. .send_request = ohci_send_request,
  1398. .send_response = ohci_send_response,
  1399. .cancel_packet = ohci_cancel_packet,
  1400. .enable_phys_dma = ohci_enable_phys_dma,
  1401. .get_bus_time = ohci_get_bus_time,
  1402. .allocate_iso_context = ohci_allocate_iso_context,
  1403. .free_iso_context = ohci_free_iso_context,
  1404. .queue_iso = ohci_queue_iso,
  1405. .start_iso = ohci_start_iso,
  1406. .stop_iso = ohci_stop_iso,
  1407. };
  1408. static int software_reset(struct fw_ohci *ohci)
  1409. {
  1410. int i;
  1411. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1412. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1413. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1414. OHCI1394_HCControl_softReset) == 0)
  1415. return 0;
  1416. msleep(1);
  1417. }
  1418. return -EBUSY;
  1419. }
  1420. static int __devinit
  1421. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1422. {
  1423. struct fw_ohci *ohci;
  1424. u32 bus_options, max_receive, link_speed;
  1425. u64 guid;
  1426. int err;
  1427. size_t size;
  1428. ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
  1429. if (ohci == NULL) {
  1430. fw_error("Could not malloc fw_ohci data.\n");
  1431. return -ENOMEM;
  1432. }
  1433. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1434. err = pci_enable_device(dev);
  1435. if (err) {
  1436. fw_error("Failed to enable OHCI hardware.\n");
  1437. goto fail_put_card;
  1438. }
  1439. pci_set_master(dev);
  1440. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1441. pci_set_drvdata(dev, ohci);
  1442. spin_lock_init(&ohci->lock);
  1443. tasklet_init(&ohci->bus_reset_tasklet,
  1444. bus_reset_tasklet, (unsigned long)ohci);
  1445. err = pci_request_region(dev, 0, ohci_driver_name);
  1446. if (err) {
  1447. fw_error("MMIO resource unavailable\n");
  1448. goto fail_disable;
  1449. }
  1450. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1451. if (ohci->registers == NULL) {
  1452. fw_error("Failed to remap registers\n");
  1453. err = -ENXIO;
  1454. goto fail_iomem;
  1455. }
  1456. if (software_reset(ohci)) {
  1457. fw_error("Failed to reset ohci card.\n");
  1458. err = -EBUSY;
  1459. goto fail_registers;
  1460. }
  1461. /*
  1462. * Now enable LPS, which we need in order to start accessing
  1463. * most of the registers. In fact, on some cards (ALI M5251),
  1464. * accessing registers in the SClk domain without LPS enabled
  1465. * will lock up the machine. Wait 50msec to make sure we have
  1466. * full link enabled.
  1467. */
  1468. reg_write(ohci, OHCI1394_HCControlSet,
  1469. OHCI1394_HCControl_LPS |
  1470. OHCI1394_HCControl_postedWriteEnable);
  1471. flush_writes(ohci);
  1472. msleep(50);
  1473. reg_write(ohci, OHCI1394_HCControlClear,
  1474. OHCI1394_HCControl_noByteSwapData);
  1475. reg_write(ohci, OHCI1394_LinkControlSet,
  1476. OHCI1394_LinkControl_rcvSelfID |
  1477. OHCI1394_LinkControl_cycleTimerEnable |
  1478. OHCI1394_LinkControl_cycleMaster);
  1479. ar_context_init(&ohci->ar_request_ctx, ohci,
  1480. OHCI1394_AsReqRcvContextControlSet);
  1481. ar_context_init(&ohci->ar_response_ctx, ohci,
  1482. OHCI1394_AsRspRcvContextControlSet);
  1483. context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
  1484. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  1485. context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
  1486. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  1487. reg_write(ohci, OHCI1394_ATRetries,
  1488. OHCI1394_MAX_AT_REQ_RETRIES |
  1489. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1490. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1491. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1492. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1493. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1494. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1495. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1496. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1497. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1498. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1499. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1500. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1501. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1502. fw_error("Out of memory for it/ir contexts.\n");
  1503. err = -ENOMEM;
  1504. goto fail_registers;
  1505. }
  1506. /* self-id dma buffer allocation */
  1507. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1508. SELF_ID_BUF_SIZE,
  1509. &ohci->self_id_bus,
  1510. GFP_KERNEL);
  1511. if (ohci->self_id_cpu == NULL) {
  1512. fw_error("Out of memory for self ID buffer.\n");
  1513. err = -ENOMEM;
  1514. goto fail_registers;
  1515. }
  1516. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1517. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1518. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1519. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1520. reg_write(ohci, OHCI1394_IntMaskSet,
  1521. OHCI1394_selfIDComplete |
  1522. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1523. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1524. OHCI1394_isochRx | OHCI1394_isochTx |
  1525. OHCI1394_masterIntEnable |
  1526. OHCI1394_cycle64Seconds);
  1527. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1528. max_receive = (bus_options >> 12) & 0xf;
  1529. link_speed = bus_options & 0x7;
  1530. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1531. reg_read(ohci, OHCI1394_GUIDLo);
  1532. err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1533. if (err < 0)
  1534. goto fail_self_id;
  1535. ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  1536. fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
  1537. dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
  1538. return 0;
  1539. fail_self_id:
  1540. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1541. ohci->self_id_cpu, ohci->self_id_bus);
  1542. fail_registers:
  1543. kfree(ohci->it_context_list);
  1544. kfree(ohci->ir_context_list);
  1545. pci_iounmap(dev, ohci->registers);
  1546. fail_iomem:
  1547. pci_release_region(dev, 0);
  1548. fail_disable:
  1549. pci_disable_device(dev);
  1550. fail_put_card:
  1551. fw_card_put(&ohci->card);
  1552. return err;
  1553. }
  1554. static void pci_remove(struct pci_dev *dev)
  1555. {
  1556. struct fw_ohci *ohci;
  1557. ohci = pci_get_drvdata(dev);
  1558. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1559. flush_writes(ohci);
  1560. fw_core_remove_card(&ohci->card);
  1561. /*
  1562. * FIXME: Fail all pending packets here, now that the upper
  1563. * layers can't queue any more.
  1564. */
  1565. software_reset(ohci);
  1566. free_irq(dev->irq, ohci);
  1567. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1568. ohci->self_id_cpu, ohci->self_id_bus);
  1569. kfree(ohci->it_context_list);
  1570. kfree(ohci->ir_context_list);
  1571. pci_iounmap(dev, ohci->registers);
  1572. pci_release_region(dev, 0);
  1573. pci_disable_device(dev);
  1574. fw_card_put(&ohci->card);
  1575. fw_notify("Removed fw-ohci device.\n");
  1576. }
  1577. static struct pci_device_id pci_table[] = {
  1578. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1579. { }
  1580. };
  1581. MODULE_DEVICE_TABLE(pci, pci_table);
  1582. static struct pci_driver fw_ohci_pci_driver = {
  1583. .name = ohci_driver_name,
  1584. .id_table = pci_table,
  1585. .probe = pci_probe,
  1586. .remove = pci_remove,
  1587. };
  1588. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1589. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1590. MODULE_LICENSE("GPL");
  1591. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  1592. #ifndef CONFIG_IEEE1394_OHCI1394_MODULE
  1593. MODULE_ALIAS("ohci1394");
  1594. #endif
  1595. static int __init fw_ohci_init(void)
  1596. {
  1597. return pci_register_driver(&fw_ohci_pci_driver);
  1598. }
  1599. static void __exit fw_ohci_cleanup(void)
  1600. {
  1601. pci_unregister_driver(&fw_ohci_pci_driver);
  1602. }
  1603. module_init(fw_ohci_init);
  1604. module_exit(fw_ohci_cleanup);