fw-ohci.c 53 KB

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