fw-ohci.c 52 KB

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