tlclk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /*
  2. * Telecom Clock driver for Intel NetStructure(tm) MPCBL0010
  3. *
  4. * Copyright (C) 2005 Kontron Canada
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <sebastien.bouchard@ca.kontron.com> and the current
  24. * Maintainer <mark.gross@intel.com>
  25. *
  26. * Description : This is the TELECOM CLOCK module driver for the ATCA
  27. * MPCBL0010 ATCA computer.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/kernel.h> /* printk() */
  32. #include <linux/fs.h> /* everything... */
  33. #include <linux/errno.h> /* error codes */
  34. #include <linux/slab.h>
  35. #include <linux/ioport.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/smp_lock.h>
  39. #include <linux/timer.h>
  40. #include <linux/sysfs.h>
  41. #include <linux/device.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/platform_device.h>
  44. #include <asm/io.h> /* inb/outb */
  45. #include <asm/uaccess.h>
  46. MODULE_AUTHOR("Sebastien Bouchard <sebastien.bouchard@ca.kontron.com>");
  47. MODULE_LICENSE("GPL");
  48. /*Hardware Reset of the PLL */
  49. #define RESET_ON 0x00
  50. #define RESET_OFF 0x01
  51. /* MODE SELECT */
  52. #define NORMAL_MODE 0x00
  53. #define HOLDOVER_MODE 0x10
  54. #define FREERUN_MODE 0x20
  55. /* FILTER SELECT */
  56. #define FILTER_6HZ 0x04
  57. #define FILTER_12HZ 0x00
  58. /* SELECT REFERENCE FREQUENCY */
  59. #define REF_CLK1_8kHz 0x00
  60. #define REF_CLK2_19_44MHz 0x02
  61. /* Select primary or secondary redundant clock */
  62. #define PRIMARY_CLOCK 0x00
  63. #define SECONDARY_CLOCK 0x01
  64. /* CLOCK TRANSMISSION DEFINE */
  65. #define CLK_8kHz 0xff
  66. #define CLK_16_384MHz 0xfb
  67. #define CLK_1_544MHz 0x00
  68. #define CLK_2_048MHz 0x01
  69. #define CLK_4_096MHz 0x02
  70. #define CLK_6_312MHz 0x03
  71. #define CLK_8_192MHz 0x04
  72. #define CLK_19_440MHz 0x06
  73. #define CLK_8_592MHz 0x08
  74. #define CLK_11_184MHz 0x09
  75. #define CLK_34_368MHz 0x0b
  76. #define CLK_44_736MHz 0x0a
  77. /* RECEIVED REFERENCE */
  78. #define AMC_B1 0
  79. #define AMC_B2 1
  80. /* HARDWARE SWITCHING DEFINE */
  81. #define HW_ENABLE 0x80
  82. #define HW_DISABLE 0x00
  83. /* HARDWARE SWITCHING MODE DEFINE */
  84. #define PLL_HOLDOVER 0x40
  85. #define LOST_CLOCK 0x00
  86. /* ALARMS DEFINE */
  87. #define UNLOCK_MASK 0x10
  88. #define HOLDOVER_MASK 0x20
  89. #define SEC_LOST_MASK 0x40
  90. #define PRI_LOST_MASK 0x80
  91. /* INTERRUPT CAUSE DEFINE */
  92. #define PRI_LOS_01_MASK 0x01
  93. #define PRI_LOS_10_MASK 0x02
  94. #define SEC_LOS_01_MASK 0x04
  95. #define SEC_LOS_10_MASK 0x08
  96. #define HOLDOVER_01_MASK 0x10
  97. #define HOLDOVER_10_MASK 0x20
  98. #define UNLOCK_01_MASK 0x40
  99. #define UNLOCK_10_MASK 0x80
  100. struct tlclk_alarms {
  101. __u32 lost_clocks;
  102. __u32 lost_primary_clock;
  103. __u32 lost_secondary_clock;
  104. __u32 primary_clock_back;
  105. __u32 secondary_clock_back;
  106. __u32 switchover_primary;
  107. __u32 switchover_secondary;
  108. __u32 pll_holdover;
  109. __u32 pll_end_holdover;
  110. __u32 pll_lost_sync;
  111. __u32 pll_sync;
  112. };
  113. /* Telecom clock I/O register definition */
  114. #define TLCLK_BASE 0xa08
  115. #define TLCLK_REG0 TLCLK_BASE
  116. #define TLCLK_REG1 (TLCLK_BASE+1)
  117. #define TLCLK_REG2 (TLCLK_BASE+2)
  118. #define TLCLK_REG3 (TLCLK_BASE+3)
  119. #define TLCLK_REG4 (TLCLK_BASE+4)
  120. #define TLCLK_REG5 (TLCLK_BASE+5)
  121. #define TLCLK_REG6 (TLCLK_BASE+6)
  122. #define TLCLK_REG7 (TLCLK_BASE+7)
  123. #define SET_PORT_BITS(port, mask, val) outb(((inb(port) & mask) | val), port)
  124. /* 0 = Dynamic allocation of the major device number */
  125. #define TLCLK_MAJOR 0
  126. /* sysfs interface definition:
  127. Upon loading the driver will create a sysfs directory under
  128. /sys/devices/platform/telco_clock.
  129. This directory exports the following interfaces. There operation is
  130. documented in the MCPBL0010 TPS under the Telecom Clock API section, 11.4.
  131. alarms :
  132. current_ref :
  133. received_ref_clk3a :
  134. received_ref_clk3b :
  135. enable_clk3a_output :
  136. enable_clk3b_output :
  137. enable_clka0_output :
  138. enable_clka1_output :
  139. enable_clkb0_output :
  140. enable_clkb1_output :
  141. filter_select :
  142. hardware_switching :
  143. hardware_switching_mode :
  144. telclock_version :
  145. mode_select :
  146. refalign :
  147. reset :
  148. select_amcb1_transmit_clock :
  149. select_amcb2_transmit_clock :
  150. select_redundant_clock :
  151. select_ref_frequency :
  152. All sysfs interfaces are integers in hex format, i.e echo 99 > refalign
  153. has the same effect as echo 0x99 > refalign.
  154. */
  155. static unsigned int telclk_interrupt;
  156. static int int_events; /* Event that generate a interrupt */
  157. static int got_event; /* if events processing have been done */
  158. static void switchover_timeout(unsigned long data);
  159. static struct timer_list switchover_timer =
  160. TIMER_INITIALIZER(switchover_timeout , 0, 0);
  161. static unsigned long tlclk_timer_data;
  162. static struct tlclk_alarms *alarm_events;
  163. static DEFINE_SPINLOCK(event_lock);
  164. static int tlclk_major = TLCLK_MAJOR;
  165. static irqreturn_t tlclk_interrupt(int irq, void *dev_id);
  166. static DECLARE_WAIT_QUEUE_HEAD(wq);
  167. static unsigned long useflags;
  168. static DEFINE_MUTEX(tlclk_mutex);
  169. static int tlclk_open(struct inode *inode, struct file *filp)
  170. {
  171. int result;
  172. lock_kernel();
  173. if (test_and_set_bit(0, &useflags)) {
  174. result = -EBUSY;
  175. /* this legacy device is always one per system and it doesn't
  176. * know how to handle multiple concurrent clients.
  177. */
  178. goto out;
  179. }
  180. /* Make sure there is no interrupt pending while
  181. * initialising interrupt handler */
  182. inb(TLCLK_REG6);
  183. /* This device is wired through the FPGA IO space of the ATCA blade
  184. * we can't share this IRQ */
  185. result = request_irq(telclk_interrupt, &tlclk_interrupt,
  186. IRQF_DISABLED, "telco_clock", tlclk_interrupt);
  187. if (result == -EBUSY)
  188. printk(KERN_ERR "tlclk: Interrupt can't be reserved.\n");
  189. else
  190. inb(TLCLK_REG6); /* Clear interrupt events */
  191. out:
  192. unlock_kernel();
  193. return result;
  194. }
  195. static int tlclk_release(struct inode *inode, struct file *filp)
  196. {
  197. free_irq(telclk_interrupt, tlclk_interrupt);
  198. clear_bit(0, &useflags);
  199. return 0;
  200. }
  201. static ssize_t tlclk_read(struct file *filp, char __user *buf, size_t count,
  202. loff_t *f_pos)
  203. {
  204. if (count < sizeof(struct tlclk_alarms))
  205. return -EIO;
  206. if (mutex_lock_interruptible(&tlclk_mutex))
  207. return -EINTR;
  208. wait_event_interruptible(wq, got_event);
  209. if (copy_to_user(buf, alarm_events, sizeof(struct tlclk_alarms))) {
  210. mutex_unlock(&tlclk_mutex);
  211. return -EFAULT;
  212. }
  213. memset(alarm_events, 0, sizeof(struct tlclk_alarms));
  214. got_event = 0;
  215. mutex_unlock(&tlclk_mutex);
  216. return sizeof(struct tlclk_alarms);
  217. }
  218. static const struct file_operations tlclk_fops = {
  219. .read = tlclk_read,
  220. .open = tlclk_open,
  221. .release = tlclk_release,
  222. };
  223. static struct miscdevice tlclk_miscdev = {
  224. .minor = MISC_DYNAMIC_MINOR,
  225. .name = "telco_clock",
  226. .fops = &tlclk_fops,
  227. };
  228. static ssize_t show_current_ref(struct device *d,
  229. struct device_attribute *attr, char *buf)
  230. {
  231. unsigned long ret_val;
  232. unsigned long flags;
  233. spin_lock_irqsave(&event_lock, flags);
  234. ret_val = ((inb(TLCLK_REG1) & 0x08) >> 3);
  235. spin_unlock_irqrestore(&event_lock, flags);
  236. return sprintf(buf, "0x%lX\n", ret_val);
  237. }
  238. static DEVICE_ATTR(current_ref, S_IRUGO, show_current_ref, NULL);
  239. static ssize_t show_telclock_version(struct device *d,
  240. struct device_attribute *attr, char *buf)
  241. {
  242. unsigned long ret_val;
  243. unsigned long flags;
  244. spin_lock_irqsave(&event_lock, flags);
  245. ret_val = inb(TLCLK_REG5);
  246. spin_unlock_irqrestore(&event_lock, flags);
  247. return sprintf(buf, "0x%lX\n", ret_val);
  248. }
  249. static DEVICE_ATTR(telclock_version, S_IRUGO,
  250. show_telclock_version, NULL);
  251. static ssize_t show_alarms(struct device *d,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. unsigned long ret_val;
  255. unsigned long flags;
  256. spin_lock_irqsave(&event_lock, flags);
  257. ret_val = (inb(TLCLK_REG2) & 0xf0);
  258. spin_unlock_irqrestore(&event_lock, flags);
  259. return sprintf(buf, "0x%lX\n", ret_val);
  260. }
  261. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  262. static ssize_t store_received_ref_clk3a(struct device *d,
  263. struct device_attribute *attr, const char *buf, size_t count)
  264. {
  265. unsigned long tmp;
  266. unsigned char val;
  267. unsigned long flags;
  268. sscanf(buf, "%lX", &tmp);
  269. dev_dbg(d, ": tmp = 0x%lX\n", tmp);
  270. val = (unsigned char)tmp;
  271. spin_lock_irqsave(&event_lock, flags);
  272. SET_PORT_BITS(TLCLK_REG1, 0xef, val);
  273. spin_unlock_irqrestore(&event_lock, flags);
  274. return strnlen(buf, count);
  275. }
  276. static DEVICE_ATTR(received_ref_clk3a, (S_IWUSR|S_IWGRP), NULL,
  277. store_received_ref_clk3a);
  278. static ssize_t store_received_ref_clk3b(struct device *d,
  279. struct device_attribute *attr, const char *buf, size_t count)
  280. {
  281. unsigned long tmp;
  282. unsigned char val;
  283. unsigned long flags;
  284. sscanf(buf, "%lX", &tmp);
  285. dev_dbg(d, ": tmp = 0x%lX\n", tmp);
  286. val = (unsigned char)tmp;
  287. spin_lock_irqsave(&event_lock, flags);
  288. SET_PORT_BITS(TLCLK_REG1, 0xdf, val << 1);
  289. spin_unlock_irqrestore(&event_lock, flags);
  290. return strnlen(buf, count);
  291. }
  292. static DEVICE_ATTR(received_ref_clk3b, (S_IWUSR|S_IWGRP), NULL,
  293. store_received_ref_clk3b);
  294. static ssize_t store_enable_clk3b_output(struct device *d,
  295. struct device_attribute *attr, const char *buf, size_t count)
  296. {
  297. unsigned long tmp;
  298. unsigned char val;
  299. unsigned long flags;
  300. sscanf(buf, "%lX", &tmp);
  301. dev_dbg(d, ": tmp = 0x%lX\n", tmp);
  302. val = (unsigned char)tmp;
  303. spin_lock_irqsave(&event_lock, flags);
  304. SET_PORT_BITS(TLCLK_REG3, 0x7f, val << 7);
  305. spin_unlock_irqrestore(&event_lock, flags);
  306. return strnlen(buf, count);
  307. }
  308. static DEVICE_ATTR(enable_clk3b_output, (S_IWUSR|S_IWGRP), NULL,
  309. store_enable_clk3b_output);
  310. static ssize_t store_enable_clk3a_output(struct device *d,
  311. struct device_attribute *attr, const char *buf, size_t count)
  312. {
  313. unsigned long flags;
  314. unsigned long tmp;
  315. unsigned char val;
  316. sscanf(buf, "%lX", &tmp);
  317. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  318. val = (unsigned char)tmp;
  319. spin_lock_irqsave(&event_lock, flags);
  320. SET_PORT_BITS(TLCLK_REG3, 0xbf, val << 6);
  321. spin_unlock_irqrestore(&event_lock, flags);
  322. return strnlen(buf, count);
  323. }
  324. static DEVICE_ATTR(enable_clk3a_output, (S_IWUSR|S_IWGRP), NULL,
  325. store_enable_clk3a_output);
  326. static ssize_t store_enable_clkb1_output(struct device *d,
  327. struct device_attribute *attr, const char *buf, size_t count)
  328. {
  329. unsigned long flags;
  330. unsigned long tmp;
  331. unsigned char val;
  332. sscanf(buf, "%lX", &tmp);
  333. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  334. val = (unsigned char)tmp;
  335. spin_lock_irqsave(&event_lock, flags);
  336. SET_PORT_BITS(TLCLK_REG2, 0xf7, val << 3);
  337. spin_unlock_irqrestore(&event_lock, flags);
  338. return strnlen(buf, count);
  339. }
  340. static DEVICE_ATTR(enable_clkb1_output, (S_IWUSR|S_IWGRP), NULL,
  341. store_enable_clkb1_output);
  342. static ssize_t store_enable_clka1_output(struct device *d,
  343. struct device_attribute *attr, const char *buf, size_t count)
  344. {
  345. unsigned long flags;
  346. unsigned long tmp;
  347. unsigned char val;
  348. sscanf(buf, "%lX", &tmp);
  349. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  350. val = (unsigned char)tmp;
  351. spin_lock_irqsave(&event_lock, flags);
  352. SET_PORT_BITS(TLCLK_REG2, 0xfb, val << 2);
  353. spin_unlock_irqrestore(&event_lock, flags);
  354. return strnlen(buf, count);
  355. }
  356. static DEVICE_ATTR(enable_clka1_output, (S_IWUSR|S_IWGRP), NULL,
  357. store_enable_clka1_output);
  358. static ssize_t store_enable_clkb0_output(struct device *d,
  359. struct device_attribute *attr, const char *buf, size_t count)
  360. {
  361. unsigned long flags;
  362. unsigned long tmp;
  363. unsigned char val;
  364. sscanf(buf, "%lX", &tmp);
  365. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  366. val = (unsigned char)tmp;
  367. spin_lock_irqsave(&event_lock, flags);
  368. SET_PORT_BITS(TLCLK_REG2, 0xfd, val << 1);
  369. spin_unlock_irqrestore(&event_lock, flags);
  370. return strnlen(buf, count);
  371. }
  372. static DEVICE_ATTR(enable_clkb0_output, (S_IWUSR|S_IWGRP), NULL,
  373. store_enable_clkb0_output);
  374. static ssize_t store_enable_clka0_output(struct device *d,
  375. struct device_attribute *attr, const char *buf, size_t count)
  376. {
  377. unsigned long flags;
  378. unsigned long tmp;
  379. unsigned char val;
  380. sscanf(buf, "%lX", &tmp);
  381. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  382. val = (unsigned char)tmp;
  383. spin_lock_irqsave(&event_lock, flags);
  384. SET_PORT_BITS(TLCLK_REG2, 0xfe, val);
  385. spin_unlock_irqrestore(&event_lock, flags);
  386. return strnlen(buf, count);
  387. }
  388. static DEVICE_ATTR(enable_clka0_output, (S_IWUSR|S_IWGRP), NULL,
  389. store_enable_clka0_output);
  390. static ssize_t store_select_amcb2_transmit_clock(struct device *d,
  391. struct device_attribute *attr, const char *buf, size_t count)
  392. {
  393. unsigned long flags;
  394. unsigned long tmp;
  395. unsigned char val;
  396. sscanf(buf, "%lX", &tmp);
  397. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  398. val = (unsigned char)tmp;
  399. spin_lock_irqsave(&event_lock, flags);
  400. if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
  401. SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x28);
  402. SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
  403. } else if (val >= CLK_8_592MHz) {
  404. SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x38);
  405. switch (val) {
  406. case CLK_8_592MHz:
  407. SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
  408. break;
  409. case CLK_11_184MHz:
  410. SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
  411. break;
  412. case CLK_34_368MHz:
  413. SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
  414. break;
  415. case CLK_44_736MHz:
  416. SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
  417. break;
  418. }
  419. } else
  420. SET_PORT_BITS(TLCLK_REG3, 0xc7, val << 3);
  421. spin_unlock_irqrestore(&event_lock, flags);
  422. return strnlen(buf, count);
  423. }
  424. static DEVICE_ATTR(select_amcb2_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
  425. store_select_amcb2_transmit_clock);
  426. static ssize_t store_select_amcb1_transmit_clock(struct device *d,
  427. struct device_attribute *attr, const char *buf, size_t count)
  428. {
  429. unsigned long tmp;
  430. unsigned char val;
  431. unsigned long flags;
  432. sscanf(buf, "%lX", &tmp);
  433. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  434. val = (unsigned char)tmp;
  435. spin_lock_irqsave(&event_lock, flags);
  436. if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
  437. SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x5);
  438. SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
  439. } else if (val >= CLK_8_592MHz) {
  440. SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x7);
  441. switch (val) {
  442. case CLK_8_592MHz:
  443. SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
  444. break;
  445. case CLK_11_184MHz:
  446. SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
  447. break;
  448. case CLK_34_368MHz:
  449. SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
  450. break;
  451. case CLK_44_736MHz:
  452. SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
  453. break;
  454. }
  455. } else
  456. SET_PORT_BITS(TLCLK_REG3, 0xf8, val);
  457. spin_unlock_irqrestore(&event_lock, flags);
  458. return strnlen(buf, count);
  459. }
  460. static DEVICE_ATTR(select_amcb1_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
  461. store_select_amcb1_transmit_clock);
  462. static ssize_t store_select_redundant_clock(struct device *d,
  463. struct device_attribute *attr, const char *buf, size_t count)
  464. {
  465. unsigned long tmp;
  466. unsigned char val;
  467. unsigned long flags;
  468. sscanf(buf, "%lX", &tmp);
  469. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  470. val = (unsigned char)tmp;
  471. spin_lock_irqsave(&event_lock, flags);
  472. SET_PORT_BITS(TLCLK_REG1, 0xfe, val);
  473. spin_unlock_irqrestore(&event_lock, flags);
  474. return strnlen(buf, count);
  475. }
  476. static DEVICE_ATTR(select_redundant_clock, (S_IWUSR|S_IWGRP), NULL,
  477. store_select_redundant_clock);
  478. static ssize_t store_select_ref_frequency(struct device *d,
  479. struct device_attribute *attr, const char *buf, size_t count)
  480. {
  481. unsigned long tmp;
  482. unsigned char val;
  483. unsigned long flags;
  484. sscanf(buf, "%lX", &tmp);
  485. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  486. val = (unsigned char)tmp;
  487. spin_lock_irqsave(&event_lock, flags);
  488. SET_PORT_BITS(TLCLK_REG1, 0xfd, val);
  489. spin_unlock_irqrestore(&event_lock, flags);
  490. return strnlen(buf, count);
  491. }
  492. static DEVICE_ATTR(select_ref_frequency, (S_IWUSR|S_IWGRP), NULL,
  493. store_select_ref_frequency);
  494. static ssize_t store_filter_select(struct device *d,
  495. struct device_attribute *attr, const char *buf, size_t count)
  496. {
  497. unsigned long tmp;
  498. unsigned char val;
  499. unsigned long flags;
  500. sscanf(buf, "%lX", &tmp);
  501. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  502. val = (unsigned char)tmp;
  503. spin_lock_irqsave(&event_lock, flags);
  504. SET_PORT_BITS(TLCLK_REG0, 0xfb, val);
  505. spin_unlock_irqrestore(&event_lock, flags);
  506. return strnlen(buf, count);
  507. }
  508. static DEVICE_ATTR(filter_select, (S_IWUSR|S_IWGRP), NULL, store_filter_select);
  509. static ssize_t store_hardware_switching_mode(struct device *d,
  510. struct device_attribute *attr, const char *buf, size_t count)
  511. {
  512. unsigned long tmp;
  513. unsigned char val;
  514. unsigned long flags;
  515. sscanf(buf, "%lX", &tmp);
  516. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  517. val = (unsigned char)tmp;
  518. spin_lock_irqsave(&event_lock, flags);
  519. SET_PORT_BITS(TLCLK_REG0, 0xbf, val);
  520. spin_unlock_irqrestore(&event_lock, flags);
  521. return strnlen(buf, count);
  522. }
  523. static DEVICE_ATTR(hardware_switching_mode, (S_IWUSR|S_IWGRP), NULL,
  524. store_hardware_switching_mode);
  525. static ssize_t store_hardware_switching(struct device *d,
  526. struct device_attribute *attr, const char *buf, size_t count)
  527. {
  528. unsigned long tmp;
  529. unsigned char val;
  530. unsigned long flags;
  531. sscanf(buf, "%lX", &tmp);
  532. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  533. val = (unsigned char)tmp;
  534. spin_lock_irqsave(&event_lock, flags);
  535. SET_PORT_BITS(TLCLK_REG0, 0x7f, val);
  536. spin_unlock_irqrestore(&event_lock, flags);
  537. return strnlen(buf, count);
  538. }
  539. static DEVICE_ATTR(hardware_switching, (S_IWUSR|S_IWGRP), NULL,
  540. store_hardware_switching);
  541. static ssize_t store_refalign (struct device *d,
  542. struct device_attribute *attr, const char *buf, size_t count)
  543. {
  544. unsigned long tmp;
  545. unsigned long flags;
  546. sscanf(buf, "%lX", &tmp);
  547. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  548. spin_lock_irqsave(&event_lock, flags);
  549. SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
  550. SET_PORT_BITS(TLCLK_REG0, 0xf7, 0x08);
  551. SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
  552. spin_unlock_irqrestore(&event_lock, flags);
  553. return strnlen(buf, count);
  554. }
  555. static DEVICE_ATTR(refalign, (S_IWUSR|S_IWGRP), NULL, store_refalign);
  556. static ssize_t store_mode_select (struct device *d,
  557. struct device_attribute *attr, const char *buf, size_t count)
  558. {
  559. unsigned long tmp;
  560. unsigned char val;
  561. unsigned long flags;
  562. sscanf(buf, "%lX", &tmp);
  563. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  564. val = (unsigned char)tmp;
  565. spin_lock_irqsave(&event_lock, flags);
  566. SET_PORT_BITS(TLCLK_REG0, 0xcf, val);
  567. spin_unlock_irqrestore(&event_lock, flags);
  568. return strnlen(buf, count);
  569. }
  570. static DEVICE_ATTR(mode_select, (S_IWUSR|S_IWGRP), NULL, store_mode_select);
  571. static ssize_t store_reset (struct device *d,
  572. struct device_attribute *attr, const char *buf, size_t count)
  573. {
  574. unsigned long tmp;
  575. unsigned char val;
  576. unsigned long flags;
  577. sscanf(buf, "%lX", &tmp);
  578. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  579. val = (unsigned char)tmp;
  580. spin_lock_irqsave(&event_lock, flags);
  581. SET_PORT_BITS(TLCLK_REG4, 0xfd, val);
  582. spin_unlock_irqrestore(&event_lock, flags);
  583. return strnlen(buf, count);
  584. }
  585. static DEVICE_ATTR(reset, (S_IWUSR|S_IWGRP), NULL, store_reset);
  586. static struct attribute *tlclk_sysfs_entries[] = {
  587. &dev_attr_current_ref.attr,
  588. &dev_attr_telclock_version.attr,
  589. &dev_attr_alarms.attr,
  590. &dev_attr_received_ref_clk3a.attr,
  591. &dev_attr_received_ref_clk3b.attr,
  592. &dev_attr_enable_clk3a_output.attr,
  593. &dev_attr_enable_clk3b_output.attr,
  594. &dev_attr_enable_clkb1_output.attr,
  595. &dev_attr_enable_clka1_output.attr,
  596. &dev_attr_enable_clkb0_output.attr,
  597. &dev_attr_enable_clka0_output.attr,
  598. &dev_attr_select_amcb1_transmit_clock.attr,
  599. &dev_attr_select_amcb2_transmit_clock.attr,
  600. &dev_attr_select_redundant_clock.attr,
  601. &dev_attr_select_ref_frequency.attr,
  602. &dev_attr_filter_select.attr,
  603. &dev_attr_hardware_switching_mode.attr,
  604. &dev_attr_hardware_switching.attr,
  605. &dev_attr_refalign.attr,
  606. &dev_attr_mode_select.attr,
  607. &dev_attr_reset.attr,
  608. NULL
  609. };
  610. static struct attribute_group tlclk_attribute_group = {
  611. .name = NULL, /* put in device directory */
  612. .attrs = tlclk_sysfs_entries,
  613. };
  614. static struct platform_device *tlclk_device;
  615. static int __init tlclk_init(void)
  616. {
  617. int ret;
  618. ret = register_chrdev(tlclk_major, "telco_clock", &tlclk_fops);
  619. if (ret < 0) {
  620. printk(KERN_ERR "tlclk: can't get major %d.\n", tlclk_major);
  621. return ret;
  622. }
  623. tlclk_major = ret;
  624. alarm_events = kzalloc( sizeof(struct tlclk_alarms), GFP_KERNEL);
  625. if (!alarm_events)
  626. goto out1;
  627. /* Read telecom clock IRQ number (Set by BIOS) */
  628. if (!request_region(TLCLK_BASE, 8, "telco_clock")) {
  629. printk(KERN_ERR "tlclk: request_region 0x%X failed.\n",
  630. TLCLK_BASE);
  631. ret = -EBUSY;
  632. goto out2;
  633. }
  634. telclk_interrupt = (inb(TLCLK_REG7) & 0x0f);
  635. if (0x0F == telclk_interrupt ) { /* not MCPBL0010 ? */
  636. printk(KERN_ERR "telclk_interrup = 0x%x non-mcpbl0010 hw.\n",
  637. telclk_interrupt);
  638. ret = -ENXIO;
  639. goto out3;
  640. }
  641. init_timer(&switchover_timer);
  642. ret = misc_register(&tlclk_miscdev);
  643. if (ret < 0) {
  644. printk(KERN_ERR "tlclk: misc_register returns %d.\n", ret);
  645. goto out3;
  646. }
  647. tlclk_device = platform_device_register_simple("telco_clock",
  648. -1, NULL, 0);
  649. if (IS_ERR(tlclk_device)) {
  650. printk(KERN_ERR "tlclk: platform_device_register failed.\n");
  651. ret = PTR_ERR(tlclk_device);
  652. goto out4;
  653. }
  654. ret = sysfs_create_group(&tlclk_device->dev.kobj,
  655. &tlclk_attribute_group);
  656. if (ret) {
  657. printk(KERN_ERR "tlclk: failed to create sysfs device attributes.\n");
  658. goto out5;
  659. }
  660. return 0;
  661. out5:
  662. platform_device_unregister(tlclk_device);
  663. out4:
  664. misc_deregister(&tlclk_miscdev);
  665. out3:
  666. release_region(TLCLK_BASE, 8);
  667. out2:
  668. kfree(alarm_events);
  669. out1:
  670. unregister_chrdev(tlclk_major, "telco_clock");
  671. return ret;
  672. }
  673. static void __exit tlclk_cleanup(void)
  674. {
  675. sysfs_remove_group(&tlclk_device->dev.kobj, &tlclk_attribute_group);
  676. platform_device_unregister(tlclk_device);
  677. misc_deregister(&tlclk_miscdev);
  678. unregister_chrdev(tlclk_major, "telco_clock");
  679. release_region(TLCLK_BASE, 8);
  680. del_timer_sync(&switchover_timer);
  681. kfree(alarm_events);
  682. }
  683. static void switchover_timeout(unsigned long data)
  684. {
  685. unsigned long flags = *(unsigned long *) data;
  686. if ((flags & 1)) {
  687. if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
  688. alarm_events->switchover_primary++;
  689. } else {
  690. if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
  691. alarm_events->switchover_secondary++;
  692. }
  693. /* Alarm processing is done, wake up read task */
  694. del_timer(&switchover_timer);
  695. got_event = 1;
  696. wake_up(&wq);
  697. }
  698. static irqreturn_t tlclk_interrupt(int irq, void *dev_id)
  699. {
  700. unsigned long flags;
  701. spin_lock_irqsave(&event_lock, flags);
  702. /* Read and clear interrupt events */
  703. int_events = inb(TLCLK_REG6);
  704. /* Primary_Los changed from 0 to 1 ? */
  705. if (int_events & PRI_LOS_01_MASK) {
  706. if (inb(TLCLK_REG2) & SEC_LOST_MASK)
  707. alarm_events->lost_clocks++;
  708. else
  709. alarm_events->lost_primary_clock++;
  710. }
  711. /* Primary_Los changed from 1 to 0 ? */
  712. if (int_events & PRI_LOS_10_MASK) {
  713. alarm_events->primary_clock_back++;
  714. SET_PORT_BITS(TLCLK_REG1, 0xFE, 1);
  715. }
  716. /* Secondary_Los changed from 0 to 1 ? */
  717. if (int_events & SEC_LOS_01_MASK) {
  718. if (inb(TLCLK_REG2) & PRI_LOST_MASK)
  719. alarm_events->lost_clocks++;
  720. else
  721. alarm_events->lost_secondary_clock++;
  722. }
  723. /* Secondary_Los changed from 1 to 0 ? */
  724. if (int_events & SEC_LOS_10_MASK) {
  725. alarm_events->secondary_clock_back++;
  726. SET_PORT_BITS(TLCLK_REG1, 0xFE, 0);
  727. }
  728. if (int_events & HOLDOVER_10_MASK)
  729. alarm_events->pll_end_holdover++;
  730. if (int_events & UNLOCK_01_MASK)
  731. alarm_events->pll_lost_sync++;
  732. if (int_events & UNLOCK_10_MASK)
  733. alarm_events->pll_sync++;
  734. /* Holdover changed from 0 to 1 ? */
  735. if (int_events & HOLDOVER_01_MASK) {
  736. alarm_events->pll_holdover++;
  737. /* TIMEOUT in ~10ms */
  738. switchover_timer.expires = jiffies + msecs_to_jiffies(10);
  739. tlclk_timer_data = inb(TLCLK_REG1);
  740. switchover_timer.data = (unsigned long) &tlclk_timer_data;
  741. mod_timer(&switchover_timer, switchover_timer.expires);
  742. } else {
  743. got_event = 1;
  744. wake_up(&wq);
  745. }
  746. spin_unlock_irqrestore(&event_lock, flags);
  747. return IRQ_HANDLED;
  748. }
  749. module_init(tlclk_init);
  750. module_exit(tlclk_cleanup);