xref: /freebsd/sys/dev/ow/owll_if.m (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1ae1f3df4SWarner Losh#-
2*f86e6000SWarner Losh# Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
3ae1f3df4SWarner Losh#
4ae1f3df4SWarner Losh# Redistribution and use in source and binary forms, with or without
5ae1f3df4SWarner Losh# modification, are permitted provided that the following conditions
6ae1f3df4SWarner Losh# are met:
7ae1f3df4SWarner Losh# 1. Redistributions of source code must retain the above copyright
8ae1f3df4SWarner Losh#    notice, this list of conditions and the following disclaimer.
9ae1f3df4SWarner Losh# 2. Redistributions in binary form must reproduce the above copyright
10ae1f3df4SWarner Losh#    notice, this list of conditions and the following disclaimer in the
11ae1f3df4SWarner Losh#    documentation and/or other materials provided with the distribution.
12ae1f3df4SWarner Losh#
13ae1f3df4SWarner Losh# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ae1f3df4SWarner Losh# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ae1f3df4SWarner Losh# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ae1f3df4SWarner Losh# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ae1f3df4SWarner Losh# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ae1f3df4SWarner Losh# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ae1f3df4SWarner Losh# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ae1f3df4SWarner Losh# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ae1f3df4SWarner Losh# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ae1f3df4SWarner Losh# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ae1f3df4SWarner Losh# SUCH DAMAGE.
24ae1f3df4SWarner Losh#
25ae1f3df4SWarner Losh#
26ae1f3df4SWarner Losh
27ae1f3df4SWarner Losh#include <sys/bus.h>
28ae1f3df4SWarner Losh#include <dev/ow/owll.h>
29ae1f3df4SWarner Losh
30ae1f3df4SWarner LoshINTERFACE owll;
31ae1f3df4SWarner Losh
32ae1f3df4SWarner Losh#
33ae1f3df4SWarner Losh# Dallas Semiconductor 1-Wire bus Link Layer (owll)
34ae1f3df4SWarner Losh#
35ae1f3df4SWarner Losh# See Maxim Application Note AN937: Book of iButton Standards for the
36ae1f3df4SWarner Losh# 1-Wire protocol specification.
37ae1f3df4SWarner Losh# http://pdfserv.maximintegrated.com/en/an/AN937.pdf
38ae1f3df4SWarner Losh#
39ae1f3df4SWarner Losh# Note: 1-Wire is a registered trademark of Maxim Integrated Products, Inc.
40ae1f3df4SWarner Losh#
41ae1f3df4SWarner Losh# This file provides an interface to the logical layer of the protocol.
42ae1f3df4SWarner Losh# Although the first implementation is done with GPIO bit banging, some
43ae1f3df4SWarner Losh# SoCs have a 1-Wire controller with more smarts or hardware offload.
44ae1f3df4SWarner Losh# Maxim datasheets also describe how to use UARTs to generate timing,
45ae1f3df4SWarner Losh# as well as both usb and i2c 1-Wire controllers.
46ae1f3df4SWarner Losh#
47ae1f3df4SWarner Losh# Chapter 4 has all the electrical timing diagrams that make up the link
48ae1f3df4SWarner Losh# layer of this protocol.
49ae1f3df4SWarner Losh#
50ae1f3df4SWarner Losh# Two speed classes are defined: Regular speed and Overdrive speed.
51ae1f3df4SWarner Losh# It is the responsibility of a device implementing the owll(9) interface
52ae1f3df4SWarner Losh# to ensure that the timings are met:
53ae1f3df4SWarner Losh#
54ae1f3df4SWarner Losh# 	Regular				Overdrive
55ae1f3df4SWarner Losh#
56ae1f3df4SWarner Losh#	60us <= tSLOT < 120us		6us <= tSLOT <= 16us
57ae1f3df4SWarner Losh#	60us <= tLOW0 < tSLOT < 120us	6us <= tLOW0 < tSLOT < 16us
58ae1f3df4SWarner Losh#	1us <= tLOW1 < 15us		1us <= tLOW < 2us
59ae1f3df4SWarner Losh#	1us < tLOWR < 15us		1us <= tLOWR < 2us
60ae1f3df4SWarner Losh#	0 <= tRELEASE < 45us		0 <= tRELEASE < 4us
61ae1f3df4SWarner Losh#	1us <= tREC < inf		1us <= tREC < inf
62ae1f3df4SWarner Losh#	tRDV = 15us			tRDV = 2us
63ae1f3df4SWarner Losh#	480us <= tRSTL < inf		48us <= tRSTL < 80us
64ae1f3df4SWarner Losh#	480us <= tRSTH < inf		48us <= tRSTH < inf
65ae1f3df4SWarner Losh#	15us < tPDH < 60us		2us <= tPDH < 6us
66ae1f3df4SWarner Losh#	60us < tPDL < 240us		8us <= tPDL < 24us
67ae1f3df4SWarner Losh#
68ae1f3df4SWarner Losh# In the diagrams below, R is driven by the resistor pullup, M is driven by
69ae1f3df4SWarner Losh# the master, and S is driven by the slave / target.
70ae1f3df4SWarner Losh#
71ae1f3df4SWarner Losh# All of these methods are expected to be called from the "network"/bus layer
72ae1f3df4SWarner Losh# for doing its operations. See 1wn_if.m for those.
73ae1f3df4SWarner Losh#
74ae1f3df4SWarner Losh# Note: This is the polling / busy-wait interface. An interrupt-based interface
75ae1f3df4SWarner Losh# may be different. But an interrupt-based, non-blocking interface can be tricky.
76ae1f3df4SWarner Losh#
77ae1f3df4SWarner Losh# Only the owbus should talk to this interface.
78ae1f3df4SWarner Losh#
79ae1f3df4SWarner Losh
80ae1f3df4SWarner Losh# WRITE-ONE (see above for timings) From Figure 4-1 AN-937
81ae1f3df4SWarner Losh#
82ae1f3df4SWarner Losh#		       |<---------tSLOT---->|<-tREC->|
83ae1f3df4SWarner Losh#	High	RRRRM  | 	RRRRRRRRRRRR|RRRRRRRRM
84ae1f3df4SWarner Losh#		     M |       R |     |  |	      M
85ae1f3df4SWarner Losh#		      M|      R	 |     |  |	       M
86ae1f3df4SWarner Losh#	Low	       MMMMMMM	 |     |  |    	        MMMMMM...
87ae1f3df4SWarner Losh#      	       	       |<-tLOW1->|     |  |
88ae1f3df4SWarner Losh#    		       |<------15us--->|  |
89ae1f3df4SWarner Losh#                      |<--------60us---->|
90ae1f3df4SWarner Losh#
91ae1f3df4SWarner Losh#
92ae1f3df4SWarner LoshMETHOD int write_one {
93ae1f3df4SWarner Losh	device_t	lldev;		/* Link Level device (eg bridge) */
94ae1f3df4SWarner Losh	struct ow_timing *timing;	/* timing values */
95ae1f3df4SWarner Losh};
96ae1f3df4SWarner Losh
97ae1f3df4SWarner Losh
98ae1f3df4SWarner Losh# WRITE-ZERO (see above for timings) From Figure 4-2 AN-937
99ae1f3df4SWarner Losh#
100ae1f3df4SWarner Losh#		       |<---------tSLOT------>|<-tREC->|
101ae1f3df4SWarner Losh#	High	RRRRM  | 	            | |RRRRRRRM
102ae1f3df4SWarner Losh#		     M |                    | R	       M
103ae1f3df4SWarner Losh#		      M|       	 |     |    |R 	        M
104ae1f3df4SWarner Losh#	Low	       MMMMMMMMMMMMMMMMMMMMMR  	         MMMMMM...
105ae1f3df4SWarner Losh#      	       	       |<--15us->|     |    |
106ae1f3df4SWarner Losh#      	       	       |<------60us--->|    |
107ae1f3df4SWarner Losh#                      |<-------tLOW0------>|
108ae1f3df4SWarner Losh#
109ae1f3df4SWarner Losh#
110ae1f3df4SWarner LoshMETHOD int write_zero {
111ae1f3df4SWarner Losh	device_t	lldev;		/* Link Level device (eg bridge) */
112ae1f3df4SWarner Losh	struct ow_timing *timing;	/* timing values */
113ae1f3df4SWarner Losh};
114ae1f3df4SWarner Losh
115ae1f3df4SWarner Losh# READ-DATA (see above for timings) From Figure 4-3 AN-937
116ae1f3df4SWarner Losh#
117ae1f3df4SWarner Losh#		       |<---------tSLOT------>|<-tREC->|
118ae1f3df4SWarner Losh#	High	RRRRM  |        rrrrrrrrrrrrrrrRRRRRRRM
119ae1f3df4SWarner Losh#		     M |       r            | R	       M
120ae1f3df4SWarner Losh#		      M|      r	        |   |R 	        M
121ae1f3df4SWarner Losh#	Low	       MMMMMMMSSSSSSSSSSSSSSR  	         MMMMMM...
122ae1f3df4SWarner Losh#      	       	       |<tLOWR>< sample	>   |
123ae1f3df4SWarner Losh#      	       	       |<------tRDV---->|   |
124ae1f3df4SWarner Losh#                                     ->|   |<-tRELEASE
125ae1f3df4SWarner Losh#
126ae1f3df4SWarner Losh# r -- allowed to pull high via the resistor when slave writes a 1-bit
127ae1f3df4SWarner Losh#
128ae1f3df4SWarner LoshMETHOD int read_data {
129ae1f3df4SWarner Losh	device_t	lldev;		/* Link Level device (eg bridge) */
130ae1f3df4SWarner Losh	struct ow_timing *timing;	/* timing values */
131ae1f3df4SWarner Losh	int		*bit;		/* Bit we sampled */
132ae1f3df4SWarner Losh};
133ae1f3df4SWarner Losh
134ae1f3df4SWarner Losh# RESET AND PRESENCE PULSE (see above for timings) From Figure 4-4 AN-937
135ae1f3df4SWarner Losh#
136ae1f3df4SWarner Losh#				    |<---------tRSTH------------>|
137ae1f3df4SWarner Losh#	High RRRM  |		  | RRRRRRRS	       |  RRRR RRM
138ae1f3df4SWarner Losh#		 M |		  |R|  	   |S  	       | R	  M
139ae1f3df4SWarner Losh#		  M|		  R |	   | S	       |R	   M
140ae1f3df4SWarner Losh#	Low	   MMMMMMMM MMMMMM| |	   |  SSSSSSSSSS	    MMMMMM
141ae1f3df4SWarner Losh#      	       	   |<----tRSTL--->| |  	   |<-tPDL---->|
142ae1f3df4SWarner Losh#		   |   	       	->| |<-tR  |	       |
143ae1f3df4SWarner Losh#				    |<tPDH>|
144ae1f3df4SWarner Losh#
145ae1f3df4SWarner Losh# Note: for Regular Speed operations, tRSTL + tR should be less than 960us to
146ae1f3df4SWarner Losh# avoid interfering with other devives on the bus.
147ae1f3df4SWarner Losh#
148ae1f3df4SWarner Losh# Returns errors associating with acquiring the bus, or EIO to indicate
149ae1f3df4SWarner Losh# that the bus was low during the RRRR time where it should have been
150ae1f3df4SWarner Losh# pulled high. The present field is always updated, even on error.
151ae1f3df4SWarner Losh#
152ae1f3df4SWarner LoshMETHOD int reset_and_presence {
153ae1f3df4SWarner Losh	device_t	lldev;		/* Link level device (eg bridge) */
154ae1f3df4SWarner Losh	struct ow_timing *timing;	/* timing values */
155ae1f3df4SWarner Losh	int		*present;	/* 0 = slave 1 = no slave -1 = bus error */
156ae1f3df4SWarner Losh};
157