fw-ohci.c 59 KB

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