fw-ohci.c 58 KB

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