fw-ohci.c 54 KB

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