fw-ohci.c 54 KB

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