ixp4xx_hss.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Intel IXP4xx HSS (synchronous serial port) driver for Linux
  3. *
  4. * Copyright (C) 2007-2008 Krzysztof Hałasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/bitops.h>
  13. #include <linux/cdev.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/fs.h>
  17. #include <linux/hdlc.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/poll.h>
  22. #include <linux/slab.h>
  23. #include <mach/npe.h>
  24. #include <mach/qmgr.h>
  25. #define DEBUG_DESC 0
  26. #define DEBUG_RX 0
  27. #define DEBUG_TX 0
  28. #define DEBUG_PKT_BYTES 0
  29. #define DEBUG_CLOSE 0
  30. #define DRV_NAME "ixp4xx_hss"
  31. #define PKT_EXTRA_FLAGS 0 /* orig 1 */
  32. #define PKT_NUM_PIPES 1 /* 1, 2 or 4 */
  33. #define PKT_PIPE_FIFO_SIZEW 4 /* total 4 dwords per HSS */
  34. #define RX_DESCS 16 /* also length of all RX queues */
  35. #define TX_DESCS 16 /* also length of all TX queues */
  36. #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
  37. #define RX_SIZE (HDLC_MAX_MRU + 4) /* NPE needs more space */
  38. #define MAX_CLOSE_WAIT 1000 /* microseconds */
  39. #define HSS_COUNT 2
  40. #define FRAME_SIZE 256 /* doesn't matter at this point */
  41. #define FRAME_OFFSET 0
  42. #define MAX_CHANNELS (FRAME_SIZE / 8)
  43. #define NAPI_WEIGHT 16
  44. /* Queue IDs */
  45. #define HSS0_CHL_RXTRIG_QUEUE 12 /* orig size = 32 dwords */
  46. #define HSS0_PKT_RX_QUEUE 13 /* orig size = 32 dwords */
  47. #define HSS0_PKT_TX0_QUEUE 14 /* orig size = 16 dwords */
  48. #define HSS0_PKT_TX1_QUEUE 15
  49. #define HSS0_PKT_TX2_QUEUE 16
  50. #define HSS0_PKT_TX3_QUEUE 17
  51. #define HSS0_PKT_RXFREE0_QUEUE 18 /* orig size = 16 dwords */
  52. #define HSS0_PKT_RXFREE1_QUEUE 19
  53. #define HSS0_PKT_RXFREE2_QUEUE 20
  54. #define HSS0_PKT_RXFREE3_QUEUE 21
  55. #define HSS0_PKT_TXDONE_QUEUE 22 /* orig size = 64 dwords */
  56. #define HSS1_CHL_RXTRIG_QUEUE 10
  57. #define HSS1_PKT_RX_QUEUE 0
  58. #define HSS1_PKT_TX0_QUEUE 5
  59. #define HSS1_PKT_TX1_QUEUE 6
  60. #define HSS1_PKT_TX2_QUEUE 7
  61. #define HSS1_PKT_TX3_QUEUE 8
  62. #define HSS1_PKT_RXFREE0_QUEUE 1
  63. #define HSS1_PKT_RXFREE1_QUEUE 2
  64. #define HSS1_PKT_RXFREE2_QUEUE 3
  65. #define HSS1_PKT_RXFREE3_QUEUE 4
  66. #define HSS1_PKT_TXDONE_QUEUE 9
  67. #define NPE_PKT_MODE_HDLC 0
  68. #define NPE_PKT_MODE_RAW 1
  69. #define NPE_PKT_MODE_56KMODE 2
  70. #define NPE_PKT_MODE_56KENDIAN_MSB 4
  71. /* PKT_PIPE_HDLC_CFG_WRITE flags */
  72. #define PKT_HDLC_IDLE_ONES 0x1 /* default = flags */
  73. #define PKT_HDLC_CRC_32 0x2 /* default = CRC-16 */
  74. #define PKT_HDLC_MSB_ENDIAN 0x4 /* default = LE */
  75. /* hss_config, PCRs */
  76. /* Frame sync sampling, default = active low */
  77. #define PCR_FRM_SYNC_ACTIVE_HIGH 0x40000000
  78. #define PCR_FRM_SYNC_FALLINGEDGE 0x80000000
  79. #define PCR_FRM_SYNC_RISINGEDGE 0xC0000000
  80. /* Frame sync pin: input (default) or output generated off a given clk edge */
  81. #define PCR_FRM_SYNC_OUTPUT_FALLING 0x20000000
  82. #define PCR_FRM_SYNC_OUTPUT_RISING 0x30000000
  83. /* Frame and data clock sampling on edge, default = falling */
  84. #define PCR_FCLK_EDGE_RISING 0x08000000
  85. #define PCR_DCLK_EDGE_RISING 0x04000000
  86. /* Clock direction, default = input */
  87. #define PCR_SYNC_CLK_DIR_OUTPUT 0x02000000
  88. /* Generate/Receive frame pulses, default = enabled */
  89. #define PCR_FRM_PULSE_DISABLED 0x01000000
  90. /* Data rate is full (default) or half the configured clk speed */
  91. #define PCR_HALF_CLK_RATE 0x00200000
  92. /* Invert data between NPE and HSS FIFOs? (default = no) */
  93. #define PCR_DATA_POLARITY_INVERT 0x00100000
  94. /* TX/RX endianness, default = LSB */
  95. #define PCR_MSB_ENDIAN 0x00080000
  96. /* Normal (default) / open drain mode (TX only) */
  97. #define PCR_TX_PINS_OPEN_DRAIN 0x00040000
  98. /* No framing bit transmitted and expected on RX? (default = framing bit) */
  99. #define PCR_SOF_NO_FBIT 0x00020000
  100. /* Drive data pins? */
  101. #define PCR_TX_DATA_ENABLE 0x00010000
  102. /* Voice 56k type: drive the data pins low (default), high, high Z */
  103. #define PCR_TX_V56K_HIGH 0x00002000
  104. #define PCR_TX_V56K_HIGH_IMP 0x00004000
  105. /* Unassigned type: drive the data pins low (default), high, high Z */
  106. #define PCR_TX_UNASS_HIGH 0x00000800
  107. #define PCR_TX_UNASS_HIGH_IMP 0x00001000
  108. /* T1 @ 1.544MHz only: Fbit dictated in FIFO (default) or high Z */
  109. #define PCR_TX_FB_HIGH_IMP 0x00000400
  110. /* 56k data endiannes - which bit unused: high (default) or low */
  111. #define PCR_TX_56KE_BIT_0_UNUSED 0x00000200
  112. /* 56k data transmission type: 32/8 bit data (default) or 56K data */
  113. #define PCR_TX_56KS_56K_DATA 0x00000100
  114. /* hss_config, cCR */
  115. /* Number of packetized clients, default = 1 */
  116. #define CCR_NPE_HFIFO_2_HDLC 0x04000000
  117. #define CCR_NPE_HFIFO_3_OR_4HDLC 0x08000000
  118. /* default = no loopback */
  119. #define CCR_LOOPBACK 0x02000000
  120. /* HSS number, default = 0 (first) */
  121. #define CCR_SECOND_HSS 0x01000000
  122. /* hss_config, clkCR: main:10, num:10, denom:12 */
  123. #define CLK42X_SPEED_EXP ((0x3FF << 22) | ( 2 << 12) | 15) /*65 KHz*/
  124. #define CLK42X_SPEED_512KHZ (( 130 << 22) | ( 2 << 12) | 15)
  125. #define CLK42X_SPEED_1536KHZ (( 43 << 22) | ( 18 << 12) | 47)
  126. #define CLK42X_SPEED_1544KHZ (( 43 << 22) | ( 33 << 12) | 192)
  127. #define CLK42X_SPEED_2048KHZ (( 32 << 22) | ( 34 << 12) | 63)
  128. #define CLK42X_SPEED_4096KHZ (( 16 << 22) | ( 34 << 12) | 127)
  129. #define CLK42X_SPEED_8192KHZ (( 8 << 22) | ( 34 << 12) | 255)
  130. #define CLK46X_SPEED_512KHZ (( 130 << 22) | ( 24 << 12) | 127)
  131. #define CLK46X_SPEED_1536KHZ (( 43 << 22) | (152 << 12) | 383)
  132. #define CLK46X_SPEED_1544KHZ (( 43 << 22) | ( 66 << 12) | 385)
  133. #define CLK46X_SPEED_2048KHZ (( 32 << 22) | (280 << 12) | 511)
  134. #define CLK46X_SPEED_4096KHZ (( 16 << 22) | (280 << 12) | 1023)
  135. #define CLK46X_SPEED_8192KHZ (( 8 << 22) | (280 << 12) | 2047)
  136. /*
  137. * HSS_CONFIG_CLOCK_CR register consists of 3 parts:
  138. * A (10 bits), B (10 bits) and C (12 bits).
  139. * IXP42x HSS clock generator operation (verified with an oscilloscope):
  140. * Each clock bit takes 7.5 ns (1 / 133.xx MHz).
  141. * The clock sequence consists of (C - B) states of 0s and 1s, each state is
  142. * A bits wide. It's followed by (B + 1) states of 0s and 1s, each state is
  143. * (A + 1) bits wide.
  144. *
  145. * The resulting average clock frequency (assuming 33.333 MHz oscillator) is:
  146. * freq = 66.666 MHz / (A + (B + 1) / (C + 1))
  147. * minimum freq = 66.666 MHz / (A + 1)
  148. * maximum freq = 66.666 MHz / A
  149. *
  150. * Example: A = 2, B = 2, C = 7, CLOCK_CR register = 2 << 22 | 2 << 12 | 7
  151. * freq = 66.666 MHz / (2 + (2 + 1) / (7 + 1)) = 28.07 MHz (Mb/s).
  152. * The clock sequence is: 1100110011 (5 doubles) 000111000 (3 triples).
  153. * The sequence takes (C - B) * A + (B + 1) * (A + 1) = 5 * 2 + 3 * 3 bits
  154. * = 19 bits (each 7.5 ns long) = 142.5 ns (then the sequence repeats).
  155. * The sequence consists of 4 complete clock periods, thus the average
  156. * frequency (= clock rate) is 4 / 142.5 ns = 28.07 MHz (Mb/s).
  157. * (max specified clock rate for IXP42x HSS is 8.192 Mb/s).
  158. */
  159. /* hss_config, LUT entries */
  160. #define TDMMAP_UNASSIGNED 0
  161. #define TDMMAP_HDLC 1 /* HDLC - packetized */
  162. #define TDMMAP_VOICE56K 2 /* Voice56K - 7-bit channelized */
  163. #define TDMMAP_VOICE64K 3 /* Voice64K - 8-bit channelized */
  164. /* offsets into HSS config */
  165. #define HSS_CONFIG_TX_PCR 0x00 /* port configuration registers */
  166. #define HSS_CONFIG_RX_PCR 0x04
  167. #define HSS_CONFIG_CORE_CR 0x08 /* loopback control, HSS# */
  168. #define HSS_CONFIG_CLOCK_CR 0x0C /* clock generator control */
  169. #define HSS_CONFIG_TX_FCR 0x10 /* frame configuration registers */
  170. #define HSS_CONFIG_RX_FCR 0x14
  171. #define HSS_CONFIG_TX_LUT 0x18 /* channel look-up tables */
  172. #define HSS_CONFIG_RX_LUT 0x38
  173. /* NPE command codes */
  174. /* writes the ConfigWord value to the location specified by offset */
  175. #define PORT_CONFIG_WRITE 0x40
  176. /* triggers the NPE to load the contents of the configuration table */
  177. #define PORT_CONFIG_LOAD 0x41
  178. /* triggers the NPE to return an HssErrorReadResponse message */
  179. #define PORT_ERROR_READ 0x42
  180. /* triggers the NPE to reset internal status and enable the HssPacketized
  181. operation for the flow specified by pPipe */
  182. #define PKT_PIPE_FLOW_ENABLE 0x50
  183. #define PKT_PIPE_FLOW_DISABLE 0x51
  184. #define PKT_NUM_PIPES_WRITE 0x52
  185. #define PKT_PIPE_FIFO_SIZEW_WRITE 0x53
  186. #define PKT_PIPE_HDLC_CFG_WRITE 0x54
  187. #define PKT_PIPE_IDLE_PATTERN_WRITE 0x55
  188. #define PKT_PIPE_RX_SIZE_WRITE 0x56
  189. #define PKT_PIPE_MODE_WRITE 0x57
  190. /* HDLC packet status values - desc->status */
  191. #define ERR_SHUTDOWN 1 /* stop or shutdown occurrence */
  192. #define ERR_HDLC_ALIGN 2 /* HDLC alignment error */
  193. #define ERR_HDLC_FCS 3 /* HDLC Frame Check Sum error */
  194. #define ERR_RXFREE_Q_EMPTY 4 /* RX-free queue became empty while receiving
  195. this packet (if buf_len < pkt_len) */
  196. #define ERR_HDLC_TOO_LONG 5 /* HDLC frame size too long */
  197. #define ERR_HDLC_ABORT 6 /* abort sequence received */
  198. #define ERR_DISCONNECTING 7 /* disconnect is in progress */
  199. #ifdef __ARMEB__
  200. typedef struct sk_buff buffer_t;
  201. #define free_buffer dev_kfree_skb
  202. #define free_buffer_irq dev_kfree_skb_irq
  203. #else
  204. typedef void buffer_t;
  205. #define free_buffer kfree
  206. #define free_buffer_irq kfree
  207. #endif
  208. struct port {
  209. struct device *dev;
  210. struct npe *npe;
  211. struct net_device *netdev;
  212. struct napi_struct napi;
  213. struct hss_plat_info *plat;
  214. buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
  215. struct desc *desc_tab; /* coherent */
  216. u32 desc_tab_phys;
  217. unsigned int id;
  218. unsigned int clock_type, clock_rate, loopback;
  219. unsigned int initialized, carrier;
  220. u8 hdlc_cfg;
  221. u32 clock_reg;
  222. };
  223. /* NPE message structure */
  224. struct msg {
  225. #ifdef __ARMEB__
  226. u8 cmd, unused, hss_port, index;
  227. union {
  228. struct { u8 data8a, data8b, data8c, data8d; };
  229. struct { u16 data16a, data16b; };
  230. struct { u32 data32; };
  231. };
  232. #else
  233. u8 index, hss_port, unused, cmd;
  234. union {
  235. struct { u8 data8d, data8c, data8b, data8a; };
  236. struct { u16 data16b, data16a; };
  237. struct { u32 data32; };
  238. };
  239. #endif
  240. };
  241. /* HDLC packet descriptor */
  242. struct desc {
  243. u32 next; /* pointer to next buffer, unused */
  244. #ifdef __ARMEB__
  245. u16 buf_len; /* buffer length */
  246. u16 pkt_len; /* packet length */
  247. u32 data; /* pointer to data buffer in RAM */
  248. u8 status;
  249. u8 error_count;
  250. u16 __reserved;
  251. #else
  252. u16 pkt_len; /* packet length */
  253. u16 buf_len; /* buffer length */
  254. u32 data; /* pointer to data buffer in RAM */
  255. u16 __reserved;
  256. u8 error_count;
  257. u8 status;
  258. #endif
  259. u32 __reserved1[4];
  260. };
  261. #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
  262. (n) * sizeof(struct desc))
  263. #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
  264. #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
  265. ((n) + RX_DESCS) * sizeof(struct desc))
  266. #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
  267. /*****************************************************************************
  268. * global variables
  269. ****************************************************************************/
  270. static int ports_open;
  271. static struct dma_pool *dma_pool;
  272. static spinlock_t npe_lock;
  273. static const struct {
  274. int tx, txdone, rx, rxfree;
  275. }queue_ids[2] = {{HSS0_PKT_TX0_QUEUE, HSS0_PKT_TXDONE_QUEUE, HSS0_PKT_RX_QUEUE,
  276. HSS0_PKT_RXFREE0_QUEUE},
  277. {HSS1_PKT_TX0_QUEUE, HSS1_PKT_TXDONE_QUEUE, HSS1_PKT_RX_QUEUE,
  278. HSS1_PKT_RXFREE0_QUEUE},
  279. };
  280. /*****************************************************************************
  281. * utility functions
  282. ****************************************************************************/
  283. static inline struct port* dev_to_port(struct net_device *dev)
  284. {
  285. return dev_to_hdlc(dev)->priv;
  286. }
  287. #ifndef __ARMEB__
  288. static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
  289. {
  290. int i;
  291. for (i = 0; i < cnt; i++)
  292. dest[i] = swab32(src[i]);
  293. }
  294. #endif
  295. /*****************************************************************************
  296. * HSS access
  297. ****************************************************************************/
  298. static void hss_npe_send(struct port *port, struct msg *msg, const char* what)
  299. {
  300. u32 *val = (u32*)msg;
  301. if (npe_send_message(port->npe, msg, what)) {
  302. pr_crit("HSS-%i: unable to send command [%08X:%08X] to %s\n",
  303. port->id, val[0], val[1], npe_name(port->npe));
  304. BUG();
  305. }
  306. }
  307. static void hss_config_set_lut(struct port *port)
  308. {
  309. struct msg msg;
  310. int ch;
  311. memset(&msg, 0, sizeof(msg));
  312. msg.cmd = PORT_CONFIG_WRITE;
  313. msg.hss_port = port->id;
  314. for (ch = 0; ch < MAX_CHANNELS; ch++) {
  315. msg.data32 >>= 2;
  316. msg.data32 |= TDMMAP_HDLC << 30;
  317. if (ch % 16 == 15) {
  318. msg.index = HSS_CONFIG_TX_LUT + ((ch / 4) & ~3);
  319. hss_npe_send(port, &msg, "HSS_SET_TX_LUT");
  320. msg.index += HSS_CONFIG_RX_LUT - HSS_CONFIG_TX_LUT;
  321. hss_npe_send(port, &msg, "HSS_SET_RX_LUT");
  322. }
  323. }
  324. }
  325. static void hss_config(struct port *port)
  326. {
  327. struct msg msg;
  328. memset(&msg, 0, sizeof(msg));
  329. msg.cmd = PORT_CONFIG_WRITE;
  330. msg.hss_port = port->id;
  331. msg.index = HSS_CONFIG_TX_PCR;
  332. msg.data32 = PCR_FRM_PULSE_DISABLED | PCR_MSB_ENDIAN |
  333. PCR_TX_DATA_ENABLE | PCR_SOF_NO_FBIT;
  334. if (port->clock_type == CLOCK_INT)
  335. msg.data32 |= PCR_SYNC_CLK_DIR_OUTPUT;
  336. hss_npe_send(port, &msg, "HSS_SET_TX_PCR");
  337. msg.index = HSS_CONFIG_RX_PCR;
  338. msg.data32 ^= PCR_TX_DATA_ENABLE | PCR_DCLK_EDGE_RISING;
  339. hss_npe_send(port, &msg, "HSS_SET_RX_PCR");
  340. memset(&msg, 0, sizeof(msg));
  341. msg.cmd = PORT_CONFIG_WRITE;
  342. msg.hss_port = port->id;
  343. msg.index = HSS_CONFIG_CORE_CR;
  344. msg.data32 = (port->loopback ? CCR_LOOPBACK : 0) |
  345. (port->id ? CCR_SECOND_HSS : 0);
  346. hss_npe_send(port, &msg, "HSS_SET_CORE_CR");
  347. memset(&msg, 0, sizeof(msg));
  348. msg.cmd = PORT_CONFIG_WRITE;
  349. msg.hss_port = port->id;
  350. msg.index = HSS_CONFIG_CLOCK_CR;
  351. msg.data32 = port->clock_reg;
  352. hss_npe_send(port, &msg, "HSS_SET_CLOCK_CR");
  353. memset(&msg, 0, sizeof(msg));
  354. msg.cmd = PORT_CONFIG_WRITE;
  355. msg.hss_port = port->id;
  356. msg.index = HSS_CONFIG_TX_FCR;
  357. msg.data16a = FRAME_OFFSET;
  358. msg.data16b = FRAME_SIZE - 1;
  359. hss_npe_send(port, &msg, "HSS_SET_TX_FCR");
  360. memset(&msg, 0, sizeof(msg));
  361. msg.cmd = PORT_CONFIG_WRITE;
  362. msg.hss_port = port->id;
  363. msg.index = HSS_CONFIG_RX_FCR;
  364. msg.data16a = FRAME_OFFSET;
  365. msg.data16b = FRAME_SIZE - 1;
  366. hss_npe_send(port, &msg, "HSS_SET_RX_FCR");
  367. hss_config_set_lut(port);
  368. memset(&msg, 0, sizeof(msg));
  369. msg.cmd = PORT_CONFIG_LOAD;
  370. msg.hss_port = port->id;
  371. hss_npe_send(port, &msg, "HSS_LOAD_CONFIG");
  372. if (npe_recv_message(port->npe, &msg, "HSS_LOAD_CONFIG") ||
  373. /* HSS_LOAD_CONFIG for port #1 returns port_id = #4 */
  374. msg.cmd != PORT_CONFIG_LOAD || msg.data32) {
  375. pr_crit("HSS-%i: HSS_LOAD_CONFIG failed\n", port->id);
  376. BUG();
  377. }
  378. /* HDLC may stop working without this - check FIXME */
  379. npe_recv_message(port->npe, &msg, "FLUSH_IT");
  380. }
  381. static void hss_set_hdlc_cfg(struct port *port)
  382. {
  383. struct msg msg;
  384. memset(&msg, 0, sizeof(msg));
  385. msg.cmd = PKT_PIPE_HDLC_CFG_WRITE;
  386. msg.hss_port = port->id;
  387. msg.data8a = port->hdlc_cfg; /* rx_cfg */
  388. msg.data8b = port->hdlc_cfg | (PKT_EXTRA_FLAGS << 3); /* tx_cfg */
  389. hss_npe_send(port, &msg, "HSS_SET_HDLC_CFG");
  390. }
  391. static u32 hss_get_status(struct port *port)
  392. {
  393. struct msg msg;
  394. memset(&msg, 0, sizeof(msg));
  395. msg.cmd = PORT_ERROR_READ;
  396. msg.hss_port = port->id;
  397. hss_npe_send(port, &msg, "PORT_ERROR_READ");
  398. if (npe_recv_message(port->npe, &msg, "PORT_ERROR_READ")) {
  399. pr_crit("HSS-%i: unable to read HSS status\n", port->id);
  400. BUG();
  401. }
  402. return msg.data32;
  403. }
  404. static void hss_start_hdlc(struct port *port)
  405. {
  406. struct msg msg;
  407. memset(&msg, 0, sizeof(msg));
  408. msg.cmd = PKT_PIPE_FLOW_ENABLE;
  409. msg.hss_port = port->id;
  410. msg.data32 = 0;
  411. hss_npe_send(port, &msg, "HSS_ENABLE_PKT_PIPE");
  412. }
  413. static void hss_stop_hdlc(struct port *port)
  414. {
  415. struct msg msg;
  416. memset(&msg, 0, sizeof(msg));
  417. msg.cmd = PKT_PIPE_FLOW_DISABLE;
  418. msg.hss_port = port->id;
  419. hss_npe_send(port, &msg, "HSS_DISABLE_PKT_PIPE");
  420. hss_get_status(port); /* make sure it's halted */
  421. }
  422. static int hss_load_firmware(struct port *port)
  423. {
  424. struct msg msg;
  425. int err;
  426. if (port->initialized)
  427. return 0;
  428. if (!npe_running(port->npe) &&
  429. (err = npe_load_firmware(port->npe, npe_name(port->npe),
  430. port->dev)))
  431. return err;
  432. /* HDLC mode configuration */
  433. memset(&msg, 0, sizeof(msg));
  434. msg.cmd = PKT_NUM_PIPES_WRITE;
  435. msg.hss_port = port->id;
  436. msg.data8a = PKT_NUM_PIPES;
  437. hss_npe_send(port, &msg, "HSS_SET_PKT_PIPES");
  438. msg.cmd = PKT_PIPE_FIFO_SIZEW_WRITE;
  439. msg.data8a = PKT_PIPE_FIFO_SIZEW;
  440. hss_npe_send(port, &msg, "HSS_SET_PKT_FIFO");
  441. msg.cmd = PKT_PIPE_MODE_WRITE;
  442. msg.data8a = NPE_PKT_MODE_HDLC;
  443. /* msg.data8b = inv_mask */
  444. /* msg.data8c = or_mask */
  445. hss_npe_send(port, &msg, "HSS_SET_PKT_MODE");
  446. msg.cmd = PKT_PIPE_RX_SIZE_WRITE;
  447. msg.data16a = HDLC_MAX_MRU; /* including CRC */
  448. hss_npe_send(port, &msg, "HSS_SET_PKT_RX_SIZE");
  449. msg.cmd = PKT_PIPE_IDLE_PATTERN_WRITE;
  450. msg.data32 = 0x7F7F7F7F; /* ??? FIXME */
  451. hss_npe_send(port, &msg, "HSS_SET_PKT_IDLE");
  452. port->initialized = 1;
  453. return 0;
  454. }
  455. /*****************************************************************************
  456. * packetized (HDLC) operation
  457. ****************************************************************************/
  458. static inline void debug_pkt(struct net_device *dev, const char *func,
  459. u8 *data, int len)
  460. {
  461. #if DEBUG_PKT_BYTES
  462. int i;
  463. printk(KERN_DEBUG "%s: %s(%i)", dev->name, func, len);
  464. for (i = 0; i < len; i++) {
  465. if (i >= DEBUG_PKT_BYTES)
  466. break;
  467. printk("%s%02X", !(i % 4) ? " " : "", data[i]);
  468. }
  469. printk("\n");
  470. #endif
  471. }
  472. static inline void debug_desc(u32 phys, struct desc *desc)
  473. {
  474. #if DEBUG_DESC
  475. printk(KERN_DEBUG "%X: %X %3X %3X %08X %X %X\n",
  476. phys, desc->next, desc->buf_len, desc->pkt_len,
  477. desc->data, desc->status, desc->error_count);
  478. #endif
  479. }
  480. static inline int queue_get_desc(unsigned int queue, struct port *port,
  481. int is_tx)
  482. {
  483. u32 phys, tab_phys, n_desc;
  484. struct desc *tab;
  485. if (!(phys = qmgr_get_entry(queue)))
  486. return -1;
  487. BUG_ON(phys & 0x1F);
  488. tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
  489. tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
  490. n_desc = (phys - tab_phys) / sizeof(struct desc);
  491. BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
  492. debug_desc(phys, &tab[n_desc]);
  493. BUG_ON(tab[n_desc].next);
  494. return n_desc;
  495. }
  496. static inline void queue_put_desc(unsigned int queue, u32 phys,
  497. struct desc *desc)
  498. {
  499. debug_desc(phys, desc);
  500. BUG_ON(phys & 0x1F);
  501. qmgr_put_entry(queue, phys);
  502. /* Don't check for queue overflow here, we've allocated sufficient
  503. length and queues >= 32 don't support this check anyway. */
  504. }
  505. static inline void dma_unmap_tx(struct port *port, struct desc *desc)
  506. {
  507. #ifdef __ARMEB__
  508. dma_unmap_single(&port->netdev->dev, desc->data,
  509. desc->buf_len, DMA_TO_DEVICE);
  510. #else
  511. dma_unmap_single(&port->netdev->dev, desc->data & ~3,
  512. ALIGN((desc->data & 3) + desc->buf_len, 4),
  513. DMA_TO_DEVICE);
  514. #endif
  515. }
  516. static void hss_hdlc_set_carrier(void *pdev, int carrier)
  517. {
  518. struct net_device *netdev = pdev;
  519. struct port *port = dev_to_port(netdev);
  520. unsigned long flags;
  521. spin_lock_irqsave(&npe_lock, flags);
  522. port->carrier = carrier;
  523. if (!port->loopback) {
  524. if (carrier)
  525. netif_carrier_on(netdev);
  526. else
  527. netif_carrier_off(netdev);
  528. }
  529. spin_unlock_irqrestore(&npe_lock, flags);
  530. }
  531. static void hss_hdlc_rx_irq(void *pdev)
  532. {
  533. struct net_device *dev = pdev;
  534. struct port *port = dev_to_port(dev);
  535. #if DEBUG_RX
  536. printk(KERN_DEBUG "%s: hss_hdlc_rx_irq\n", dev->name);
  537. #endif
  538. qmgr_disable_irq(queue_ids[port->id].rx);
  539. napi_schedule(&port->napi);
  540. }
  541. static int hss_hdlc_poll(struct napi_struct *napi, int budget)
  542. {
  543. struct port *port = container_of(napi, struct port, napi);
  544. struct net_device *dev = port->netdev;
  545. unsigned int rxq = queue_ids[port->id].rx;
  546. unsigned int rxfreeq = queue_ids[port->id].rxfree;
  547. int received = 0;
  548. #if DEBUG_RX
  549. printk(KERN_DEBUG "%s: hss_hdlc_poll\n", dev->name);
  550. #endif
  551. while (received < budget) {
  552. struct sk_buff *skb;
  553. struct desc *desc;
  554. int n;
  555. #ifdef __ARMEB__
  556. struct sk_buff *temp;
  557. u32 phys;
  558. #endif
  559. if ((n = queue_get_desc(rxq, port, 0)) < 0) {
  560. #if DEBUG_RX
  561. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  562. " napi_complete\n", dev->name);
  563. #endif
  564. napi_complete(napi);
  565. qmgr_enable_irq(rxq);
  566. if (!qmgr_stat_empty(rxq) &&
  567. napi_reschedule(napi)) {
  568. #if DEBUG_RX
  569. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  570. " napi_reschedule succeeded\n",
  571. dev->name);
  572. #endif
  573. qmgr_disable_irq(rxq);
  574. continue;
  575. }
  576. #if DEBUG_RX
  577. printk(KERN_DEBUG "%s: hss_hdlc_poll all done\n",
  578. dev->name);
  579. #endif
  580. return received; /* all work done */
  581. }
  582. desc = rx_desc_ptr(port, n);
  583. #if 0 /* FIXME - error_count counts modulo 256, perhaps we should use it */
  584. if (desc->error_count)
  585. printk(KERN_DEBUG "%s: hss_hdlc_poll status 0x%02X"
  586. " errors %u\n", dev->name, desc->status,
  587. desc->error_count);
  588. #endif
  589. skb = NULL;
  590. switch (desc->status) {
  591. case 0:
  592. #ifdef __ARMEB__
  593. if ((skb = netdev_alloc_skb(dev, RX_SIZE)) != NULL) {
  594. phys = dma_map_single(&dev->dev, skb->data,
  595. RX_SIZE,
  596. DMA_FROM_DEVICE);
  597. if (dma_mapping_error(&dev->dev, phys)) {
  598. dev_kfree_skb(skb);
  599. skb = NULL;
  600. }
  601. }
  602. #else
  603. skb = netdev_alloc_skb(dev, desc->pkt_len);
  604. #endif
  605. if (!skb)
  606. dev->stats.rx_dropped++;
  607. break;
  608. case ERR_HDLC_ALIGN:
  609. case ERR_HDLC_ABORT:
  610. dev->stats.rx_frame_errors++;
  611. dev->stats.rx_errors++;
  612. break;
  613. case ERR_HDLC_FCS:
  614. dev->stats.rx_crc_errors++;
  615. dev->stats.rx_errors++;
  616. break;
  617. case ERR_HDLC_TOO_LONG:
  618. dev->stats.rx_length_errors++;
  619. dev->stats.rx_errors++;
  620. break;
  621. default: /* FIXME - remove printk */
  622. netdev_err(dev, "hss_hdlc_poll: status 0x%02X errors %u\n",
  623. desc->status, desc->error_count);
  624. dev->stats.rx_errors++;
  625. }
  626. if (!skb) {
  627. /* put the desc back on RX-ready queue */
  628. desc->buf_len = RX_SIZE;
  629. desc->pkt_len = desc->status = 0;
  630. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  631. continue;
  632. }
  633. /* process received frame */
  634. #ifdef __ARMEB__
  635. temp = skb;
  636. skb = port->rx_buff_tab[n];
  637. dma_unmap_single(&dev->dev, desc->data,
  638. RX_SIZE, DMA_FROM_DEVICE);
  639. #else
  640. dma_sync_single_for_cpu(&dev->dev, desc->data,
  641. RX_SIZE, DMA_FROM_DEVICE);
  642. memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
  643. ALIGN(desc->pkt_len, 4) / 4);
  644. #endif
  645. skb_put(skb, desc->pkt_len);
  646. debug_pkt(dev, "hss_hdlc_poll", skb->data, skb->len);
  647. skb->protocol = hdlc_type_trans(skb, dev);
  648. dev->stats.rx_packets++;
  649. dev->stats.rx_bytes += skb->len;
  650. netif_receive_skb(skb);
  651. /* put the new buffer on RX-free queue */
  652. #ifdef __ARMEB__
  653. port->rx_buff_tab[n] = temp;
  654. desc->data = phys;
  655. #endif
  656. desc->buf_len = RX_SIZE;
  657. desc->pkt_len = 0;
  658. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  659. received++;
  660. }
  661. #if DEBUG_RX
  662. printk(KERN_DEBUG "hss_hdlc_poll: end, not all work done\n");
  663. #endif
  664. return received; /* not all work done */
  665. }
  666. static void hss_hdlc_txdone_irq(void *pdev)
  667. {
  668. struct net_device *dev = pdev;
  669. struct port *port = dev_to_port(dev);
  670. int n_desc;
  671. #if DEBUG_TX
  672. printk(KERN_DEBUG DRV_NAME ": hss_hdlc_txdone_irq\n");
  673. #endif
  674. while ((n_desc = queue_get_desc(queue_ids[port->id].txdone,
  675. port, 1)) >= 0) {
  676. struct desc *desc;
  677. int start;
  678. desc = tx_desc_ptr(port, n_desc);
  679. dev->stats.tx_packets++;
  680. dev->stats.tx_bytes += desc->pkt_len;
  681. dma_unmap_tx(port, desc);
  682. #if DEBUG_TX
  683. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq free %p\n",
  684. dev->name, port->tx_buff_tab[n_desc]);
  685. #endif
  686. free_buffer_irq(port->tx_buff_tab[n_desc]);
  687. port->tx_buff_tab[n_desc] = NULL;
  688. start = qmgr_stat_below_low_watermark(port->plat->txreadyq);
  689. queue_put_desc(port->plat->txreadyq,
  690. tx_desc_phys(port, n_desc), desc);
  691. if (start) { /* TX-ready queue was empty */
  692. #if DEBUG_TX
  693. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq xmit"
  694. " ready\n", dev->name);
  695. #endif
  696. netif_wake_queue(dev);
  697. }
  698. }
  699. }
  700. static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
  701. {
  702. struct port *port = dev_to_port(dev);
  703. unsigned int txreadyq = port->plat->txreadyq;
  704. int len, offset, bytes, n;
  705. void *mem;
  706. u32 phys;
  707. struct desc *desc;
  708. #if DEBUG_TX
  709. printk(KERN_DEBUG "%s: hss_hdlc_xmit\n", dev->name);
  710. #endif
  711. if (unlikely(skb->len > HDLC_MAX_MRU)) {
  712. dev_kfree_skb(skb);
  713. dev->stats.tx_errors++;
  714. return NETDEV_TX_OK;
  715. }
  716. debug_pkt(dev, "hss_hdlc_xmit", skb->data, skb->len);
  717. len = skb->len;
  718. #ifdef __ARMEB__
  719. offset = 0; /* no need to keep alignment */
  720. bytes = len;
  721. mem = skb->data;
  722. #else
  723. offset = (int)skb->data & 3; /* keep 32-bit alignment */
  724. bytes = ALIGN(offset + len, 4);
  725. if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
  726. dev_kfree_skb(skb);
  727. dev->stats.tx_dropped++;
  728. return NETDEV_TX_OK;
  729. }
  730. memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
  731. dev_kfree_skb(skb);
  732. #endif
  733. phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
  734. if (dma_mapping_error(&dev->dev, phys)) {
  735. #ifdef __ARMEB__
  736. dev_kfree_skb(skb);
  737. #else
  738. kfree(mem);
  739. #endif
  740. dev->stats.tx_dropped++;
  741. return NETDEV_TX_OK;
  742. }
  743. n = queue_get_desc(txreadyq, port, 1);
  744. BUG_ON(n < 0);
  745. desc = tx_desc_ptr(port, n);
  746. #ifdef __ARMEB__
  747. port->tx_buff_tab[n] = skb;
  748. #else
  749. port->tx_buff_tab[n] = mem;
  750. #endif
  751. desc->data = phys + offset;
  752. desc->buf_len = desc->pkt_len = len;
  753. wmb();
  754. queue_put_desc(queue_ids[port->id].tx, tx_desc_phys(port, n), desc);
  755. if (qmgr_stat_below_low_watermark(txreadyq)) { /* empty */
  756. #if DEBUG_TX
  757. printk(KERN_DEBUG "%s: hss_hdlc_xmit queue full\n", dev->name);
  758. #endif
  759. netif_stop_queue(dev);
  760. /* we could miss TX ready interrupt */
  761. if (!qmgr_stat_below_low_watermark(txreadyq)) {
  762. #if DEBUG_TX
  763. printk(KERN_DEBUG "%s: hss_hdlc_xmit ready again\n",
  764. dev->name);
  765. #endif
  766. netif_wake_queue(dev);
  767. }
  768. }
  769. #if DEBUG_TX
  770. printk(KERN_DEBUG "%s: hss_hdlc_xmit end\n", dev->name);
  771. #endif
  772. return NETDEV_TX_OK;
  773. }
  774. static int request_hdlc_queues(struct port *port)
  775. {
  776. int err;
  777. err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
  778. "%s:RX-free", port->netdev->name);
  779. if (err)
  780. return err;
  781. err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
  782. "%s:RX", port->netdev->name);
  783. if (err)
  784. goto rel_rxfree;
  785. err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
  786. "%s:TX", port->netdev->name);
  787. if (err)
  788. goto rel_rx;
  789. err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
  790. "%s:TX-ready", port->netdev->name);
  791. if (err)
  792. goto rel_tx;
  793. err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
  794. "%s:TX-done", port->netdev->name);
  795. if (err)
  796. goto rel_txready;
  797. return 0;
  798. rel_txready:
  799. qmgr_release_queue(port->plat->txreadyq);
  800. rel_tx:
  801. qmgr_release_queue(queue_ids[port->id].tx);
  802. rel_rx:
  803. qmgr_release_queue(queue_ids[port->id].rx);
  804. rel_rxfree:
  805. qmgr_release_queue(queue_ids[port->id].rxfree);
  806. printk(KERN_DEBUG "%s: unable to request hardware queues\n",
  807. port->netdev->name);
  808. return err;
  809. }
  810. static void release_hdlc_queues(struct port *port)
  811. {
  812. qmgr_release_queue(queue_ids[port->id].rxfree);
  813. qmgr_release_queue(queue_ids[port->id].rx);
  814. qmgr_release_queue(queue_ids[port->id].txdone);
  815. qmgr_release_queue(queue_ids[port->id].tx);
  816. qmgr_release_queue(port->plat->txreadyq);
  817. }
  818. static int init_hdlc_queues(struct port *port)
  819. {
  820. int i;
  821. if (!ports_open) {
  822. dma_pool = dma_pool_create(DRV_NAME, &port->netdev->dev,
  823. POOL_ALLOC_SIZE, 32, 0);
  824. if (!dma_pool)
  825. return -ENOMEM;
  826. }
  827. if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
  828. &port->desc_tab_phys)))
  829. return -ENOMEM;
  830. memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
  831. memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
  832. memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
  833. /* Setup RX buffers */
  834. for (i = 0; i < RX_DESCS; i++) {
  835. struct desc *desc = rx_desc_ptr(port, i);
  836. buffer_t *buff;
  837. void *data;
  838. #ifdef __ARMEB__
  839. if (!(buff = netdev_alloc_skb(port->netdev, RX_SIZE)))
  840. return -ENOMEM;
  841. data = buff->data;
  842. #else
  843. if (!(buff = kmalloc(RX_SIZE, GFP_KERNEL)))
  844. return -ENOMEM;
  845. data = buff;
  846. #endif
  847. desc->buf_len = RX_SIZE;
  848. desc->data = dma_map_single(&port->netdev->dev, data,
  849. RX_SIZE, DMA_FROM_DEVICE);
  850. if (dma_mapping_error(&port->netdev->dev, desc->data)) {
  851. free_buffer(buff);
  852. return -EIO;
  853. }
  854. port->rx_buff_tab[i] = buff;
  855. }
  856. return 0;
  857. }
  858. static void destroy_hdlc_queues(struct port *port)
  859. {
  860. int i;
  861. if (port->desc_tab) {
  862. for (i = 0; i < RX_DESCS; i++) {
  863. struct desc *desc = rx_desc_ptr(port, i);
  864. buffer_t *buff = port->rx_buff_tab[i];
  865. if (buff) {
  866. dma_unmap_single(&port->netdev->dev,
  867. desc->data, RX_SIZE,
  868. DMA_FROM_DEVICE);
  869. free_buffer(buff);
  870. }
  871. }
  872. for (i = 0; i < TX_DESCS; i++) {
  873. struct desc *desc = tx_desc_ptr(port, i);
  874. buffer_t *buff = port->tx_buff_tab[i];
  875. if (buff) {
  876. dma_unmap_tx(port, desc);
  877. free_buffer(buff);
  878. }
  879. }
  880. dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
  881. port->desc_tab = NULL;
  882. }
  883. if (!ports_open && dma_pool) {
  884. dma_pool_destroy(dma_pool);
  885. dma_pool = NULL;
  886. }
  887. }
  888. static int hss_hdlc_open(struct net_device *dev)
  889. {
  890. struct port *port = dev_to_port(dev);
  891. unsigned long flags;
  892. int i, err = 0;
  893. if ((err = hdlc_open(dev)))
  894. return err;
  895. if ((err = hss_load_firmware(port)))
  896. goto err_hdlc_close;
  897. if ((err = request_hdlc_queues(port)))
  898. goto err_hdlc_close;
  899. if ((err = init_hdlc_queues(port)))
  900. goto err_destroy_queues;
  901. spin_lock_irqsave(&npe_lock, flags);
  902. if (port->plat->open)
  903. if ((err = port->plat->open(port->id, dev,
  904. hss_hdlc_set_carrier)))
  905. goto err_unlock;
  906. spin_unlock_irqrestore(&npe_lock, flags);
  907. /* Populate queues with buffers, no failure after this point */
  908. for (i = 0; i < TX_DESCS; i++)
  909. queue_put_desc(port->plat->txreadyq,
  910. tx_desc_phys(port, i), tx_desc_ptr(port, i));
  911. for (i = 0; i < RX_DESCS; i++)
  912. queue_put_desc(queue_ids[port->id].rxfree,
  913. rx_desc_phys(port, i), rx_desc_ptr(port, i));
  914. napi_enable(&port->napi);
  915. netif_start_queue(dev);
  916. qmgr_set_irq(queue_ids[port->id].rx, QUEUE_IRQ_SRC_NOT_EMPTY,
  917. hss_hdlc_rx_irq, dev);
  918. qmgr_set_irq(queue_ids[port->id].txdone, QUEUE_IRQ_SRC_NOT_EMPTY,
  919. hss_hdlc_txdone_irq, dev);
  920. qmgr_enable_irq(queue_ids[port->id].txdone);
  921. ports_open++;
  922. hss_set_hdlc_cfg(port);
  923. hss_config(port);
  924. hss_start_hdlc(port);
  925. /* we may already have RX data, enables IRQ */
  926. napi_schedule(&port->napi);
  927. return 0;
  928. err_unlock:
  929. spin_unlock_irqrestore(&npe_lock, flags);
  930. err_destroy_queues:
  931. destroy_hdlc_queues(port);
  932. release_hdlc_queues(port);
  933. err_hdlc_close:
  934. hdlc_close(dev);
  935. return err;
  936. }
  937. static int hss_hdlc_close(struct net_device *dev)
  938. {
  939. struct port *port = dev_to_port(dev);
  940. unsigned long flags;
  941. int i, buffs = RX_DESCS; /* allocated RX buffers */
  942. spin_lock_irqsave(&npe_lock, flags);
  943. ports_open--;
  944. qmgr_disable_irq(queue_ids[port->id].rx);
  945. netif_stop_queue(dev);
  946. napi_disable(&port->napi);
  947. hss_stop_hdlc(port);
  948. while (queue_get_desc(queue_ids[port->id].rxfree, port, 0) >= 0)
  949. buffs--;
  950. while (queue_get_desc(queue_ids[port->id].rx, port, 0) >= 0)
  951. buffs--;
  952. if (buffs)
  953. netdev_crit(dev, "unable to drain RX queue, %i buffer(s) left in NPE\n",
  954. buffs);
  955. buffs = TX_DESCS;
  956. while (queue_get_desc(queue_ids[port->id].tx, port, 1) >= 0)
  957. buffs--; /* cancel TX */
  958. i = 0;
  959. do {
  960. while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
  961. buffs--;
  962. if (!buffs)
  963. break;
  964. } while (++i < MAX_CLOSE_WAIT);
  965. if (buffs)
  966. netdev_crit(dev, "unable to drain TX queue, %i buffer(s) left in NPE\n",
  967. buffs);
  968. #if DEBUG_CLOSE
  969. if (!buffs)
  970. printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
  971. #endif
  972. qmgr_disable_irq(queue_ids[port->id].txdone);
  973. if (port->plat->close)
  974. port->plat->close(port->id, dev);
  975. spin_unlock_irqrestore(&npe_lock, flags);
  976. destroy_hdlc_queues(port);
  977. release_hdlc_queues(port);
  978. hdlc_close(dev);
  979. return 0;
  980. }
  981. static int hss_hdlc_attach(struct net_device *dev, unsigned short encoding,
  982. unsigned short parity)
  983. {
  984. struct port *port = dev_to_port(dev);
  985. if (encoding != ENCODING_NRZ)
  986. return -EINVAL;
  987. switch(parity) {
  988. case PARITY_CRC16_PR1_CCITT:
  989. port->hdlc_cfg = 0;
  990. return 0;
  991. case PARITY_CRC32_PR1_CCITT:
  992. port->hdlc_cfg = PKT_HDLC_CRC_32;
  993. return 0;
  994. default:
  995. return -EINVAL;
  996. }
  997. }
  998. static u32 check_clock(u32 rate, u32 a, u32 b, u32 c,
  999. u32 *best, u32 *best_diff, u32 *reg)
  1000. {
  1001. /* a is 10-bit, b is 10-bit, c is 12-bit */
  1002. u64 new_rate;
  1003. u32 new_diff;
  1004. new_rate = ixp4xx_timer_freq * (u64)(c + 1);
  1005. do_div(new_rate, a * (c + 1) + b + 1);
  1006. new_diff = abs((u32)new_rate - rate);
  1007. if (new_diff < *best_diff) {
  1008. *best = new_rate;
  1009. *best_diff = new_diff;
  1010. *reg = (a << 22) | (b << 12) | c;
  1011. }
  1012. return new_diff;
  1013. }
  1014. static void find_best_clock(u32 rate, u32 *best, u32 *reg)
  1015. {
  1016. u32 a, b, diff = 0xFFFFFFFF;
  1017. a = ixp4xx_timer_freq / rate;
  1018. if (a > 0x3FF) { /* 10-bit value - we can go as slow as ca. 65 kb/s */
  1019. check_clock(rate, 0x3FF, 1, 1, best, &diff, reg);
  1020. return;
  1021. }
  1022. if (a == 0) { /* > 66.666 MHz */
  1023. a = 1; /* minimum divider is 1 (a = 0, b = 1, c = 1) */
  1024. rate = ixp4xx_timer_freq;
  1025. }
  1026. if (rate * a == ixp4xx_timer_freq) { /* don't divide by 0 later */
  1027. check_clock(rate, a - 1, 1, 1, best, &diff, reg);
  1028. return;
  1029. }
  1030. for (b = 0; b < 0x400; b++) {
  1031. u64 c = (b + 1) * (u64)rate;
  1032. do_div(c, ixp4xx_timer_freq - rate * a);
  1033. c--;
  1034. if (c >= 0xFFF) { /* 12-bit - no need to check more 'b's */
  1035. if (b == 0 && /* also try a bit higher rate */
  1036. !check_clock(rate, a - 1, 1, 1, best, &diff, reg))
  1037. return;
  1038. check_clock(rate, a, b, 0xFFF, best, &diff, reg);
  1039. return;
  1040. }
  1041. if (!check_clock(rate, a, b, c, best, &diff, reg))
  1042. return;
  1043. if (!check_clock(rate, a, b, c + 1, best, &diff, reg))
  1044. return;
  1045. }
  1046. }
  1047. static int hss_hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1048. {
  1049. const size_t size = sizeof(sync_serial_settings);
  1050. sync_serial_settings new_line;
  1051. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1052. struct port *port = dev_to_port(dev);
  1053. unsigned long flags;
  1054. int clk;
  1055. if (cmd != SIOCWANDEV)
  1056. return hdlc_ioctl(dev, ifr, cmd);
  1057. switch(ifr->ifr_settings.type) {
  1058. case IF_GET_IFACE:
  1059. ifr->ifr_settings.type = IF_IFACE_V35;
  1060. if (ifr->ifr_settings.size < size) {
  1061. ifr->ifr_settings.size = size; /* data size wanted */
  1062. return -ENOBUFS;
  1063. }
  1064. memset(&new_line, 0, sizeof(new_line));
  1065. new_line.clock_type = port->clock_type;
  1066. new_line.clock_rate = port->clock_rate;
  1067. new_line.loopback = port->loopback;
  1068. if (copy_to_user(line, &new_line, size))
  1069. return -EFAULT;
  1070. return 0;
  1071. case IF_IFACE_SYNC_SERIAL:
  1072. case IF_IFACE_V35:
  1073. if(!capable(CAP_NET_ADMIN))
  1074. return -EPERM;
  1075. if (copy_from_user(&new_line, line, size))
  1076. return -EFAULT;
  1077. clk = new_line.clock_type;
  1078. if (port->plat->set_clock)
  1079. clk = port->plat->set_clock(port->id, clk);
  1080. if (clk != CLOCK_EXT && clk != CLOCK_INT)
  1081. return -EINVAL; /* No such clock setting */
  1082. if (new_line.loopback != 0 && new_line.loopback != 1)
  1083. return -EINVAL;
  1084. port->clock_type = clk; /* Update settings */
  1085. if (clk == CLOCK_INT)
  1086. find_best_clock(new_line.clock_rate, &port->clock_rate,
  1087. &port->clock_reg);
  1088. else {
  1089. port->clock_rate = 0;
  1090. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1091. }
  1092. port->loopback = new_line.loopback;
  1093. spin_lock_irqsave(&npe_lock, flags);
  1094. if (dev->flags & IFF_UP)
  1095. hss_config(port);
  1096. if (port->loopback || port->carrier)
  1097. netif_carrier_on(port->netdev);
  1098. else
  1099. netif_carrier_off(port->netdev);
  1100. spin_unlock_irqrestore(&npe_lock, flags);
  1101. return 0;
  1102. default:
  1103. return hdlc_ioctl(dev, ifr, cmd);
  1104. }
  1105. }
  1106. /*****************************************************************************
  1107. * initialization
  1108. ****************************************************************************/
  1109. static const struct net_device_ops hss_hdlc_ops = {
  1110. .ndo_open = hss_hdlc_open,
  1111. .ndo_stop = hss_hdlc_close,
  1112. .ndo_change_mtu = hdlc_change_mtu,
  1113. .ndo_start_xmit = hdlc_start_xmit,
  1114. .ndo_do_ioctl = hss_hdlc_ioctl,
  1115. };
  1116. static int hss_init_one(struct platform_device *pdev)
  1117. {
  1118. struct port *port;
  1119. struct net_device *dev;
  1120. hdlc_device *hdlc;
  1121. int err;
  1122. if ((port = kzalloc(sizeof(*port), GFP_KERNEL)) == NULL)
  1123. return -ENOMEM;
  1124. if ((port->npe = npe_request(0)) == NULL) {
  1125. err = -ENODEV;
  1126. goto err_free;
  1127. }
  1128. if ((port->netdev = dev = alloc_hdlcdev(port)) == NULL) {
  1129. err = -ENOMEM;
  1130. goto err_plat;
  1131. }
  1132. SET_NETDEV_DEV(dev, &pdev->dev);
  1133. hdlc = dev_to_hdlc(dev);
  1134. hdlc->attach = hss_hdlc_attach;
  1135. hdlc->xmit = hss_hdlc_xmit;
  1136. dev->netdev_ops = &hss_hdlc_ops;
  1137. dev->tx_queue_len = 100;
  1138. port->clock_type = CLOCK_EXT;
  1139. port->clock_rate = 0;
  1140. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1141. port->id = pdev->id;
  1142. port->dev = &pdev->dev;
  1143. port->plat = pdev->dev.platform_data;
  1144. netif_napi_add(dev, &port->napi, hss_hdlc_poll, NAPI_WEIGHT);
  1145. if ((err = register_hdlc_device(dev)))
  1146. goto err_free_netdev;
  1147. platform_set_drvdata(pdev, port);
  1148. netdev_info(dev, "initialized\n");
  1149. return 0;
  1150. err_free_netdev:
  1151. free_netdev(dev);
  1152. err_plat:
  1153. npe_release(port->npe);
  1154. err_free:
  1155. kfree(port);
  1156. return err;
  1157. }
  1158. static int hss_remove_one(struct platform_device *pdev)
  1159. {
  1160. struct port *port = platform_get_drvdata(pdev);
  1161. unregister_hdlc_device(port->netdev);
  1162. free_netdev(port->netdev);
  1163. npe_release(port->npe);
  1164. kfree(port);
  1165. return 0;
  1166. }
  1167. static struct platform_driver ixp4xx_hss_driver = {
  1168. .driver.name = DRV_NAME,
  1169. .probe = hss_init_one,
  1170. .remove = hss_remove_one,
  1171. };
  1172. static int __init hss_init_module(void)
  1173. {
  1174. if ((ixp4xx_read_feature_bits() &
  1175. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS)) !=
  1176. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS))
  1177. return -ENODEV;
  1178. spin_lock_init(&npe_lock);
  1179. return platform_driver_register(&ixp4xx_hss_driver);
  1180. }
  1181. static void __exit hss_cleanup_module(void)
  1182. {
  1183. platform_driver_unregister(&ixp4xx_hss_driver);
  1184. }
  1185. MODULE_AUTHOR("Krzysztof Halasa");
  1186. MODULE_DESCRIPTION("Intel IXP4xx HSS driver");
  1187. MODULE_LICENSE("GPL v2");
  1188. MODULE_ALIAS("platform:ixp4xx_hss");
  1189. module_init(hss_init_module);
  1190. module_exit(hss_cleanup_module);