fw-ohci.c 59 KB

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