summaryrefslogtreecommitdiff
path: root/drivers/w1/masters/mxc_w1.c
blob: 7c0e4a10d23d561217703ca59a9961019f848d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @defgroup MXC_OWIRE MXC Driver for owire interface
 */

/*!
 * @file mxc_w1.c
 *
 * @brief Driver for the Freescale Semiconductor MXC owire interface.
 *
 *
 * @ingroup MXC_OWIRE
 */

/*!
 * Include Files
 */

#include <asm/atomic.h>
#include <asm/types.h>
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/pci_ids.h>
#include <linux/pci.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <mach/hardware.h>
#include <asm/setup.h>

#include "../w1.h"
#include "../w1_int.h"
#include "../w1_log.h"

/*
 * mxc function declarations
 */

static int __devinit mxc_w1_probe(struct platform_device *pdev);
static int __devexit mxc_w1_remove(struct platform_device *pdev);
static DECLARE_COMPLETION(transmit_done);
extern void gpio_owire_active(void);
extern void gpio_owire_inactive(void);

/*
 * MXC W1 Register offsets
 */
#define MXC_W1_CONTROL          0x00
#define MXC_W1_TIME_DIVIDER     0x02
#define MXC_W1_RESET            0x04
#define MXC_W1_COMMAND          0x06
#define MXC_W1_TXRX             0x08
#define MXC_W1_INTERRUPT        0x0A
#define MXC_W1_INTERRUPT_EN     0x0C
DEFINE_SPINLOCK(w1_lock);

/*!
 * This structure contains pointers to  callback functions.
 */
static struct platform_driver mxc_w1_driver = {
	.driver = {
		   .name = "mxc_w1",
		   },
	.probe = mxc_w1_probe,
	.remove = mxc_w1_remove,
};

/*!
 * This structure is used to store
 * information specific to w1 module.
 */

struct mxc_w1_device {
	char *base_address;
	unsigned long found;
	unsigned int clkdiv;
	struct clk *clk;
	struct w1_bus_master *bus_master;
};

/*
 * this is the low level routine to
 * reset the device on the One Wire interface
 * on the hardware
 * @param data  the data field of the w1 device structure
 * @return the function returns 0 when the reset pulse has
 *  been generated
 */
static u8 mxc_w1_ds2_reset_bus(void *data)
{
	volatile u8 reg_val;
	u8 ret;
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;

	__raw_writeb(0x80, (dev->base_address + MXC_W1_CONTROL));

	do {
		reg_val = __raw_readb(dev->base_address + MXC_W1_CONTROL);
	} while (((reg_val >> 7) & 0x1) != 0);
	ret = ((reg_val >> 7) & 0x1);
	return ret;
}

/*!
 * this is the low level routine to read/write a bit on the One Wire
 * interface on the hardware
 * @param data  the data field of the w1 device structure
 * @param bit  0 = write-0 cycle, 1 = write-1/read cycle
 * @return the function returns the bit read (0 or 1)
 */
static u8 mxc_w1_ds2_touch_bit(void *data, u8 bit)
{

	volatile u8 reg_val;
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;
	u8 ret = 0;

	if (0 == bit) {
		__raw_writeb((1 << 5), (dev->base_address + MXC_W1_CONTROL));

		do {
			reg_val =
			    __raw_readb(dev->base_address + MXC_W1_CONTROL);
		} while (0 != ((reg_val >> 5) & 0x1));
	}

	else {
		__raw_writeb((1 << 4), dev->base_address + MXC_W1_CONTROL);
		do {
			reg_val =
			    __raw_readb(dev->base_address + MXC_W1_CONTROL);
		} while (0 != ((reg_val >> 4) & 0x1));

		reg_val =
		    (((__raw_readb(dev->base_address + MXC_W1_CONTROL)) >> 3) &
		     0x1);
		ret = (u8) (reg_val);
	}

	return ret;
}

static void mxc_w1_ds2_write_byte(void *data, u8 byte)
{
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;
	INIT_COMPLETION(transmit_done);
	__raw_writeb(byte, (dev->base_address + MXC_W1_TXRX));
	__raw_writeb(0x10, (dev->base_address + MXC_W1_INTERRUPT_EN));
	wait_for_completion(&transmit_done);
}
static u8 mxc_w1_ds2_read_byte(void *data)
{
	volatile u8 reg_val;
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;
	mxc_w1_ds2_write_byte(data, 0xFF);
	reg_val = __raw_readb((dev->base_address + MXC_W1_TXRX));
	return reg_val;
}
static u8 mxc_w1_read_byte(void *data)
{
	volatile u8 reg_val;
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;
	reg_val = __raw_readb((dev->base_address + MXC_W1_TXRX));
	return reg_val;
}
static irqreturn_t w1_interrupt_handler(int irq, void *data)
{
	u8 reg_val;
	irqreturn_t ret = IRQ_NONE;
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;
	reg_val = __raw_readb((dev->base_address + MXC_W1_INTERRUPT));
	if ((reg_val & 0x10)) {
		complete(&transmit_done);
		reg_val = __raw_readb((dev->base_address + MXC_W1_TXRX));
		ret = IRQ_HANDLED;
	}
	return ret;
}
void search_ROM_accelerator(void *data, struct w1_master *master, u8 search_type,
			    w1_slave_found_callback cb)
{
	u64 rn[2], last_rn[2], rn2[2];
	u64 rn1, rom_id, temp, temp1;
	int i, j, z, w, last_zero, loop;
	u8 bit, reg_val, bit2;
	u8 byte, byte1;
	int disc, prev_disc, last_disc;
	struct mxc_w1_device *dev = (struct mxc_w1_device *)data;
	last_rn[0] = 0;
	last_rn[1] = 0;
	rom_id = 0;
	prev_disc = 0;
	loop = 0;
	disc = -1;
	last_disc = 0;
	last_zero = 0;
	while (!last_zero) {
		/*
		 * Reset bus and all 1-wire device state machines
		 * so they can respond to our requests.
		 *
		 * Return 0 - device(s) present, 1 - no devices present.
		 */
		if (mxc_w1_ds2_reset_bus(data)) {
			pr_debug("No devices present on the wire.\n");
			break;
		}
		rn[0] = 0;
		rn[1] = 0;
		__raw_writeb(0x80, (dev->base_address + MXC_W1_CONTROL));
		mdelay(1);
		mxc_w1_ds2_write_byte(data, 0xF0);
		__raw_writeb(0x02, (dev->base_address + MXC_W1_COMMAND));
		memcpy(rn2, last_rn, 16);
		z = 0;
		w = 0;
		for (i = 0; i < 16; i++) {
			reg_val = rn2[z] >> (8 * w);
			mxc_w1_ds2_write_byte(data, reg_val);
			reg_val = mxc_w1_read_byte(data);
			if ((reg_val & 0x3) == 0x3) {
				pr_debug("Device is Not Responding\n");
				break;
			}
			for (j = 0; j < 8; j += 2) {
				byte = 0xFF;
				byte1 = 1;
				byte ^= byte1 << j;
				bit = (reg_val >> j) & 0x1;
				bit2 = (reg_val >> j);
				if (bit) {
					prev_disc = disc;
					disc = 8 * i + j;
					reg_val &= byte;
				}
			}
			rn1 = 0;
			rn1 = reg_val;
			rn[z] |= rn1 << (8 * w);
			w++;
			if (i == 7) {
				z++;
				w = 0;
			}
		}
		if ((disc == -1) || (disc == prev_disc))
			last_zero = 1;
		if (disc == last_disc)
			disc = prev_disc;
		z = 0;
		rom_id = 0;
		for (i = 0, j = 1; i < 64; i++) {
			temp = 0;
			temp = (rn[z] >> j) & 0x1;
			rom_id |= (temp << i);
			j += 2;
			if (i == 31) {
				z++;
				j = 1;
			}

		}
		if (disc > 63) {
			last_rn[0] = rn[0];
			temp1 = rn[1];
			loop = disc % 64;
			temp = 1;
			temp1 |= (temp << (loop + 1)) - 1;
			temp1 |= (temp << (loop + 1));
			last_rn[1] = temp1;

		} else {
			last_rn[1] = 0;
			temp1 = rn[0];
			temp = 1;
			temp1 |= (temp << (loop + 1)) - 1;
			temp1 |= (temp << (loop + 1));
			last_rn[0] = temp1;
		}
		last_disc = disc;
		cb(master, rom_id);
	}
}

/*!
 * this routine sets the One Wire clock
 * to a value of 1 Mhz, as required by
 * hardware.
 * @param   dev   the device structure for w1
 * @return  The function returns void
 */
static void mxc_w1_hw_init(struct mxc_w1_device *dev)
{
	clk_enable(dev->clk);

	/* set the timer divider clock to divide by 65 */
	/* as the clock to the One Wire is at 66.5MHz */
	__raw_writeb(dev->clkdiv, dev->base_address + MXC_W1_TIME_DIVIDER);

	return;
}

/*!
 * this is the probe routine for the One Wire driver.
 * It is called during the driver initilaization.
 * @param   pdev   the platform device structure for w1
 * @return The function returns 0 on success
 * and a non-zero value on failure
 *
 */
static int __devinit mxc_w1_probe(struct platform_device *pdev)
{
	struct mxc_w1_device *dev;
	struct mxc_w1_config *data =
	    (struct mxc_w1_config *)pdev->dev.platform_data;
	int flag, ret_val, irq = -ENXIO;
	int err = 0;
	ret_val = 0;
	flag = data->search_rom_accelerator;
	dev = kzalloc(sizeof(struct mxc_w1_device) +
		      sizeof(struct w1_bus_master), GFP_KERNEL);
	if (!dev) {
		return -ENOMEM;
	}
	dev->clk = clk_get(&pdev->dev, "owire_clk");
	dev->bus_master = (struct w1_bus_master *)(dev + 1);
	dev->found = 1;
	dev->clkdiv = (clk_get_rate(dev->clk) / 1000000) - 1;
	dev->base_address = (void *)IO_ADDRESS(OWIRE_BASE_ADDR);

	mxc_w1_hw_init(dev);
	dev->bus_master->data = dev;
	dev->bus_master->reset_bus = &mxc_w1_ds2_reset_bus;
	dev->bus_master->touch_bit = &mxc_w1_ds2_touch_bit;
	if (flag) {
		dev->bus_master->write_byte = &mxc_w1_ds2_write_byte;
		dev->bus_master->read_byte = &mxc_w1_ds2_read_byte;
		dev->bus_master->search = &search_ROM_accelerator;
		irq = platform_get_irq(pdev, 0);
		if (irq < 0) {
			err = -ENOENT;
			goto err_out_free_device;
		}
		ret_val =
		    request_irq(irq, w1_interrupt_handler, 0, "mxc_w1", dev);
		if (ret_val) {
			pr_debug("OWire:request_irq(%d) returned error %d\n",
				 irq, ret_val);
			err = -1;
			goto err_out_free_device;
		}
	}
	err = w1_add_master_device(dev->bus_master);
	if (err)
		goto err_irq;

	platform_set_drvdata(pdev, dev);
	return 0;

err_irq:
	if (irq >= 0)
		free_irq(irq, dev);
err_out_free_device:
	kfree(dev);
	return err;
}

/*
 * disassociate the w1 device from the driver
 * @param   dev   the device structure for w1
 * @return  The function returns void
 */
static int mxc_w1_remove(struct platform_device *pdev)
{
	struct mxc_w1_device *dev = platform_get_drvdata(pdev);
	struct mxc_w1_config *data =
	    (struct mxc_w1_config *)pdev->dev.platform_data;
	int irq;

	if (dev->found) {
		w1_remove_master_device(dev->bus_master);
	}

	irq = platform_get_irq(pdev, 0);
	if ((irq >= 0) && (data->search_rom_accelerator))
		free_irq(irq, dev);

	clk_disable(dev->clk);
	clk_put(dev->clk);

	kfree(dev);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

/*
 * initialize the driver
 * @return The function returns 0 on success
 * and a non-zero value on failure
 */

static int __init mxc_w1_init(void)
{
	int ret;

	printk(KERN_INFO "Serial: MXC OWire driver\n");

	gpio_owire_active();

	ret = platform_driver_register(&mxc_w1_driver);

	return ret;
}

/*
 * cleanup before the driver exits
 */
static void mxc_w1_exit(void)
{
	gpio_owire_inactive();
	platform_driver_unregister(&mxc_w1_driver);
}

module_init(mxc_w1_init);
module_exit(mxc_w1_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Freescale Semiconductors Inc");
MODULE_DESCRIPTION("Driver for One-Wire on MXC");