fw-ohci.c 54 KB

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