fw-ohci.c 52 KB

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