fw-ohci.c 51 KB

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