fw-ohci.c 54 KB

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